Skip to content

Instantly share code, notes, and snippets.

@Felipe00
Last active October 20, 2015 20:53
Show Gist options
  • Save Felipe00/ff369be71af5fd1db6ee to your computer and use it in GitHub Desktop.
Save Felipe00/ff369be71af5fd1db6ee to your computer and use it in GitHub Desktop.
Exemplo de view e controller para editar um objeto.
// Recebo o id da View
public Result update(Long id) {
Form<Local> editForm = localForm.bindFromRequest();
//Busco o local pelo id (no banco)
Local localFromDb = Local.find.byId(id);
if (editForm.hasErrors()) {
LocalController.flashGlobalError(String.valueOf(editForm.get().getId()));
return badRequest(views.html.localEdit.render(localForm, id));
}
Local local = editForm.get();
//Seto o ID que veio como parâmetro no objeto que veio do form (assim não corre o risco de vir Null)
local.setId(id);
//Atualizo no banco
Ebean.update(local);
localForm.fill(new Local());
//Redireciono para outra tela
return redirect(routes.LocalController.all());
}
@* Esse parâmetro "id" vem da view de Listagem. *@
@(localForm: Form[Local], id: Long)
@import helper._
@main("Service") {
<div class="card">
<h5 class="teal-text center light">Editar Localidade</h5>
<div class="gray center">
@tags.globalErrorByFlash()
</div>
@form(routes.LocalController.update(id), 'class -> "container") {
<div class="row">
<div class="input-field col s6 center">
<input type="text" id="name" name="name" required
value="@localForm.apply("name").value" />
<label for="name">NOME DO LOCAL</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<button class="btn waves-effect waves-light right" type="submit"
name="action">
Alterar
</button>
</div>
</div>
}
</div>
}
@Alfaville
Copy link

Ex.: vc deve ter uma action Edit, onde vc retorna um ok(views.html.localEdit.render(localForm, id))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment