Why should I use the form helper and not write my own html forms

136 views Asked by At

I am learning about play 2.0 and I have got a question on the form helpers.

For me it all comes down to what benefit is it actually giving by using it in the templates? Am I using it correctly?

First: using the form helper:

@form(action = routes.Application.addAccount("blank")) {
  @inputText(accountForm("id"))
    <input type="submit" name="action" value="submit ID"/><br />
}

Why is that better then just defining

Enter your id "<input type="text" name="id"/>"

I know I can use the form model to help with validation on the server side. - that's where I see the great benefits of form helper. But where does it help to actually include the form in the Scala template? Can i use the form helper to automatically generate useful things in the html like client side validation, etc?

Cheers

1

There are 1 answers

2
František Hartman On BEST ANSWER

it helps you generate a lot more than just tags. From the documentation:

You feed them with a form field, and they display the corresponding HTML form control, with a populated value, constraints and errors

and

A rendered field does not only consist of an tag, but may also need a and a bunch of other tags used by your CSS framework to decorate the field.

(http://www.playframework.com/documentation/2.2.x/JavaFormHelpers)

So instead of

<label for=...>
... error mesages
<input ... 
</label>

you have just one readable line

@inputText(accountForm("id"))

EDIT: It will also read constraints on your java beans, e.g

@Constraints.Required
@Constraints.MinLength(5)
public String firstName;

and use html5 browser validations and display the coinstraints to the user. (http://www.playframework.com/documentation/2.2.x/JavaForms)