How to check forms with Play! in Scala?

I'm new to Play !, and I'm trying to migrate my existing website from cakePHP to Play !.

The problem I am facing is form validation.

I have defined a case User class representing users of my website:

case class User( val id: Long, val username: String, val password: String, val email: String val created: Date) 

(There are a few more fields, but these are enough to explain my problem)

I want my users to be able to create an account on my website using the form, and I would like this form to be validated on Play !.

So, I created the following action:

 def register = Action { implicit request => val userForm = Form( mapping( "id" -> longNumber, "username" -> nonEmptyText(8), "password" -> nonEmptyText(5), "email" -> email, "created" -> date)(User.apply)(User.unapply)) val processedForm = userForm.bindFromRequest processedForm.fold(hasErrors => BadRequest("Invalid submission"), success => { Ok("Account registered.") }) } 

Obviously, I do not want the user to fill in the identifier or creation date on the form. So my question is: what should I do?

Should I define a new "transition model" containing only those fields that are actually provided to the user in the form, and convert this intermediate model to the full one before inserting it into my database?

That is, replace my action with something like this:

 def register = Action { implicit request => case class UserRegister( username: String, password: String, email: String) val userForm = Form( mapping( "username" -> nonEmptyText(8), "password" -> nonEmptyText(8), "email" -> email)(UserRegister.apply)(UserRegister.unapply) val processedForm = userForm.bindFromRequest processedForm.fold(hasErrors => BadRequest("Invalid submission"), success => { val user = User(nextID, success.username, success.password, success.email, new Date()) // Register the user... Ok("Account created") } 

Or is there another, cleaner way to do what I want?

I went through a lot of tutorials and the book "Play for Scala", but in the only examples I found, the models were completely filled with forms ... I really like Play! still, but it looks like the documentation often lacks examples ...

Thanks so much for your answers!

+6
source share
1 answer

You have several options:

First, you can make the id and created Option[Long] and Option[Date] fields, respectively. Then use a mapping like this:

 val userForm = Form( mapping( "id" -> optional(longNumber), "username" -> nonEmptyText(8), "password" -> nonEmptyText(5), "email" -> email, "created" -> optional(date) )(User.apply)(User.unapply) ) 

This would be, I think, logical, since a User with the identifier None indicates that it has not yet been saved. This works well if you want to use the same form display to update an existing record.

Alternatively, you can use ignored with some arbitrary placeholder data:

 val userForm = Form( mapping( "id" -> ignored(-1L), "username" -> nonEmptyText(8), "password" -> nonEmptyText(5), "email" -> email, "created" -> ignored(new Date) )(User.apply)(User.unapply) ) 

This is not very good at reusing the form for update operations!

Finally, do not forget that your form mappings are connected / populated with functions that turn a tuple into an object and an object into a tuple, respectively. This is just a convenient convention to use the case class methods User.apply and User.unapply , because they do just that. You can write alternative factory methods for your User object to process the form instance:

 object User { def formApply(username: String, password: String, email: String): User = new User(-1L, username, password, email, new Date) def formUnapply(user: User): Option[(String,String,String)] = Some((user.username, user.password, user.email)) } 

And then the user in the Form object:

 val userForm = Form( mapping( "username" -> nonEmptyText(8), "password" -> nonEmptyText(5), "email" -> email )(User.formApply)(User.formUnapply) ) 

In addition, it is worth noting that the Scala form documentation should be much better in 2.2.1 (and in fact, it may already be deployed here ).

+4
source

Source: https://habr.com/ru/post/956830/


All Articles