[2.0.1 Java] how to implement a required field?

2,325 views
Skip to first unread message

Chris B.

unread,
Jun 26, 2012, 11:03:42 PM6/26/12
to play-fr...@googlegroups.com
I am using view helper to create an input text field.



I find no clue in this document:
http://www.playframework.org/documentation/2.0/ScalaFormHelpers

Chris B.

unread,
Jun 26, 2012, 11:07:50 PM6/26/12
to play-fr...@googlegroups.com
here is my view template code.

@inputText(contactForm("name"), '_label -> "Your Name")

How do I make this field a required field and display an inline error message when this field is blank in form submission.

Thanks for any advice.

opensource21

unread,
Jun 27, 2012, 3:22:52 AM6/27/12
to play-fr...@googlegroups.com
Have you tried to add @Required to the class which is put into the form?
Niels
Message has been deleted

Eric

unread,
Jun 27, 2012, 4:28:32 AM6/27/12
to play-fr...@googlegroups.com
Hi,

I just recently came across the same scenario and implemented validation using Bean Validation. Basically, you bind the form to some java bean that contains field-annotations like @Required, @Email, etc. In your controller class, you call form.hasErrors() to check if the bound form failed validation. Here's a snippet from a controller class that processes user registration. RegisteringUser is my java bean that contains annotations for validation. I suppose calling bindFromRequest() automatically invokes bean validation. If the form does contain errors, returning badRequest() with the filled form automatically displays inline error messages:

final static Form<RegisteringUser> registrationForm = form(RegisteringUser.class);

public static Result submit() {
    Form<RegisteringUser> filledForm = registrationForm.bindFromRequest();

    if (!filledForm.hasErrors()) {

        // Note: calling filledForm.get() when the form contains errors results in some exception being thrown.
        RegisteringUser userToRegister = filledForm.get();

        // Do stuff like save the new users to the database.
                . . .

        return ok();
    }
    else {
        logger.debug("Registration form contained errors: {}", filledForm.errors().toString());
           
        return badRequest(views.html.register.render(filledForm));
    }

}

And here's my bean:

public class RegisteringUser {
   
    @Required
    @Email
    private String email;
   
    @Required
    private String password;
   
    @Required
    private String confirmPassword;
   
    @Required
    private String name;

    // Bunch of getters and setters...
}

Hope this helps!

Chris B.

unread,
Jun 28, 2012, 11:07:41 PM6/28/12
to play-fr...@googlegroups.com
Thanks, it works. But there are some issues:.

First, it automatically add "Required" word to the end of the input box. I don't want that. How do I remove it?

Secondly, a space is not detected as blank. If you enter a space to the field, it pass the required field validation.

Lastly, how do do I customize the error message? Now I am getting "This field is required" error message when this field is left blank. I want to change the error message to another language (non-English).

My view code is like this: @inputText(contactForm("name"), '_label -> "Your Name")

Mathias Clerc

unread,
Jun 29, 2012, 3:53:04 AM6/29/12
to play-fr...@googlegroups.com
> First, it automatically add "Required" word to the end of the input box. I
> don't want that. How do I remove it?

You can disable them with a parameter.
Look for "showConstraints" in the
doc:http://www.playframework.org/documentation/2.0/JavaFormHelpers

> Secondly, a space is not detected as blank. If you enter a space to the
> field, it pass the required field validation.

You can create a special annotation for it use the ad-hoc validator.
See the doc: http://www.playframework.org/documentation/2.0/JavaForms

> Lastly, how do do I customize the error message? Now I am getting "This
> field is required" error message when this field is left blank. I want to
> change the error message to another language (non-English).

I believe they can be set with the parameters help and error.
Look for "_help" / "_error" in the
doc:http://www.playframework.org/documentation/2.0/JavaFormHelpers

Chris B.

unread,
Jun 30, 2012, 11:36:59 AM6/30/12
to play-fr...@googlegroups.com
It works except the last question. '_help will not work because it always display the message on the page. This is not what I want.

I tried this using '_error:

@inputText(contactForm("name"), '_label -> "Your Name(*)", '_showConstraints -> false, '_error -> "Invalid Name", '_showErrors -> true)

It still shows "This field is required" when the field is left blank.
<span class="help-inline">This field is required</span>

The '_error value is ignored. Did I miss anything?  I want to show "Invalid Name" to the <span class="help-inline"></span> tag. Where does it get set? Can we customize the message here?

Eric

unread,
Jul 1, 2012, 5:01:45 PM7/1/12
to play-fr...@googlegroups.com
If you want a custom message, take a look at the validation API. You can specify the message you want by passing the message element to the annotation e.g. @Required(message = "Invalid Name")

Chris B.

unread,
Jul 2, 2012, 6:12:38 PM7/2/12
to play-fr...@googlegroups.com
It works, thanks. Where is validation API? In source code or documentation?
--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/KcnPyZBMeDkJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Eric

unread,
Jul 3, 2012, 9:15:28 AM7/3/12
to play-fr...@googlegroups.com
http://bit.ly/NZZzgq


On Tuesday, July 3, 2012 12:12:38 AM UTC+2, Chris B. wrote:
It works, thanks. Where is validation API? In source code or documentation?
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages