infiniteme
infiniteme

Reputation: 195

Play 2.0/Java - Is there a way to get the validation done post request data binding?

In play 2.0 you can get the request binding with validation done (via annotations) by :

ABCForm abcForm=(ABCForm)form(ABCForm.class).bindFromRequest().get();

The problem I have is , I want to get the validation done after trimming the form values. So is there a way to either defer or call the validation stuff post binding in play 2.0 ?

Upvotes: 4

Views: 773

Answers (2)

Jay Q.
Jay Q.

Reputation: 4999

If you need to modify your values before validation. You can create a setter for your field and do your trims there.

Upvotes: 0

Leonard Punt
Leonard Punt

Reputation: 1051

Binding and validation are combined. So validation after the binding is not possible, as far as I know. However you can create a validate() method, in which you trim your values before validating them. For example:

public class User {

    public String name;

    public String validate() {
        name.trim
        if(name == "") {
            return "Name is required";
        }
        return null;
    }
}

The validate() method will be invoked when you bind a form. So you can make sure your data is valid, but errors won't be automatically added to the Form.Field objects. So it is certainly a nice solution.

There are also pretty much discussions about Form validation in Play's Google Group, so if you want to know more about the binding/validation problems I recommend reading them: https://groups.google.com/forum/#!searchin/play-framework/%5B2.0%5D$20validation.

Upvotes: 1

Related Questions