I want my form to have initial values. So I used .fill
function. but it is still giving me an empty form. what problem does my code have?
my BooksController has:
public Result edit(Integer id){
Book book = Book.findById(id);
if(book==null){
return notFound("book not found");
}
Form<Book> bookForm = formFactory.form(Book.class).fill(book);
return ok(edit.render(bookForm));
}
public Result update(Http.Request request){
Book book = formFactory.form(Book.class).bindFromRequest(request).get();
Book oldBook = Book.findById(book.id);
if(oldBook==null){return notFound("book not found");}
oldBook.title=book.title;
oldBook.author=book.author;
oldBook.price=book.price;
return redirect(routes.BooksController.front());
}
my edit.scala.html view has:
@(bookForm : Form[Book])
@import helper._
<html>
<head>
<title>edit Book</title>
</head>
<body>
<h3>edit book</h3>
@helper.form(routes.BooksController.update){
@helper.inputText(bookForm("id"))
@helper.inputText(bookForm("title"))
@helper.inputText(bookForm("author"))
@helper.inputText(bookForm("price"))
<button type="submit">edit Book</button>
}
</body>
</html>
my routes file has:
+nocsrf
GET /books/edit/:id controllers.BooksController.edit(id: Integer)
+nocsrf
POST /books/edit controllers.BooksController.update(request : Request)
As I understand from this code
oldBook.title=book.title;
oldBook.author=book.author;
oldBook.price=book.price;
You do not have getters and setters, so you need to activate "direct access" to the fields.
You can do it in the conf/application.conf
file:
play.forms.binding.directFieldAccess = true
You also can enable "direct access" only for one form by calling .withDirectFieldAccess(true)
:
Form<Book> bookForm = formFactory.form(Book.class).withDirectFieldAccess(true).fill(book);
More here: https://www.playframework.com/documentation/2.7.x/JavaForms#Defining-a-form