I am getting an error when I try to extract a user from a form with validation errors.
I have the following route configured in my routes
file:
GET /users/:user controllers.UsersController.viewUser(user: models.User)
GET /users/:user/edit controllers.UsersController.editUser(user: models.User)
This is fine at this point, and I can render a link to the user view from my scala templates:
routes.UsersController.viewUser(myUserObject)
My problem is that in my user edition form I need to get myUserObject
from a Form[User]
object. What I am currently doing is:
routes.UsersController.viewUser(userForm.get)
However, when the userForm
has any errors, the get
method raises an exception, as shown in the documentation .
The approach I have taken is passing an additional User
parameter to the scala view, together with the Form[User]
parameter I was passing up to now, I mean,
userEdit.render(user, userForm)
instead of just
userEdit.render(userForm)
However, I would like to know if there is a more suitable solution that does not involve including an additional parameter.
The documentation states that you can prefill a form with existing data:
val filledForm = userForm.fill(User("Bob", 18))
Given the preexisting User
, it should be trivial to adapt to your example.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.