Play 2.0 / Java – is there a way to complete validation after requesting data binding?

In play 2.0, you can obtain the request binding and complete the verification (via comments) in the following ways:

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

The problem I have is that I want to complete the validation after trimming the form values Is there any way to postpone or call the verification content after binding in play 2.0?

Solution

Combination and verification Therefore, as far as I know, post - binding validation is impossible However, you can create a validate () method in which you trim the 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 is called when the form is bound Therefore, you can ensure that the data is valid, but errors are not automatically added to the form Field object So it must be a good solution

There is also a lot of discussion about table validation in Google Group of play, so if you want to know more suggestions on binding / validation, I suggest you read them: https://groups.google.com/forum/# ! searchin/play-framework/%5B2. 0%5D$20validation.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>