Question

I just started playing around with Play 2.1.1 using Scala. Going through some tutorials/sample apps, I came across the helper methods that can be used to create forms, e.g.:

@(myForm: Form[User])

@helper.form(action = routes.Application.submit) {

    @helper.inputText(myForm("username"))

    @helper.inputPassword(myForm("password"))

}

I am still a n00b to this. But as far as I understand, this basically requires a form object being defined within the controller, which "wraps" the model (simplified):

  val loginForm = Form(
    tuple(
      "email" -> text,
      "password" -> text
    ))
  )

  def login = Action { implicit request =>
    Ok(html.login(loginForm))
  }

I found this suprising, as I get the feeling that the indirection through the Form object seems "in the wrong place". What I was expecting was something like this (pseudo):

@(user: User)

@helper.form(action = routes.Application.submit) {

    @helper.inputText(() => user.userName)
    @helper.inputPassword(() => user.password)
}

...so that one does not have to define the Form object within the controller; all form-related stuff would be located within the view template. Mixing the "this-will-be-rendered-into-a-form" logic into the controller seems like a minor violation of SOC to me.

Now I'm wondering: Is this just the way things are done in Play, or did I miss something? Is there a "nicer" way to handle this?

Cheers, Alex

Was it helpful?

Solution

I think it would be annoying too with many form definition in controller, especially the apps will involved many form.

But Play!Framework make developer to write code more flexible. You can mix plain HTML inside scala template helper like this :

@helper.form(action = routes.Application.submit) {
   <input type="text" name="username">
   <input type="password" name="password">

   ....
}

In my opinion, the scala helper actually helpful with form to update data that have been created before. Because it will bind the existing value into the default input value. And additionally, it also help to display error that caused by object validation.

If the form is not considered the previous value, like login form, I think the use of plain HTML input will be considered better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top