HTTP Form Submission and validation

HTTP Form Submission and validation in Play Framework

The play.data bundle holds a few assistants to handle HTTP form data submission and approval. The most effortless approach to handle a form submission is to characterize play.data.form that wraps a current class:

public class User {
public String email;
public String secret word;
}

Characterizing requirements

You can characterize extra requirements that will be checked amid the coupling stage utilizing JSR-303 (Bean Validation) annotations:

public class User {
@required
public String email;
public String secret word;
}

-The play.data.validation.constraints class holds a few implicit acceptance annotations.

You can additionally characterize an impromptu acceptance by adding an approve strategy to your top item:

public class User {
@constraints.required
public String email;
public String secret word;
public String accept() {
in the event that (authenticate(email, secret word) == invalid) {
return "Invalid data";
}
return null;
}
}

The message returned in the above case will turn into a worldwide blunder.

The accept strategy can give back where its due sorts:

String,list

Or

Map<string,list>

Accept strategy is called in the wake of checking annotation-based demands and just on the off chance that they pass. In the event that acceptance passes you must return invalid . Giving back any not-invalid worth (unfilled string or void rundown) is dealt with as fizzled approval.

List<validationerror> may be helpful when you have extra approvals for fields. Case in point:

public List accept() {
List mistakes = new Arraylist();
in the event that (User.byemail(email) != invalid) {
errors.add(new Validationerror("email", "registered now."));
}
return errors.isempty() ? invalid : mistakes;
}

Utilizing Map<string,list<validationerror>> is comparable tolist<validationerror> where map’s keys are lapse codes like email in the illustration above.

Binding Failure Handling

Obviously on the off chance that you can characterize imperatives, then you have to have the capacity to handle the coupling lapses.

If (userform.haserrors()) {
return badrequest();
} else {
Client = userform.get();
return ok("got client " + client);
}

Regularly, as demonstrated over, the form essentially gets went to a format. Worldwide slips could be rendered in the accompanying way:

@if(form.hasglobalerrors) {
@form.globalerror.message
}

Lapses for a specific field could be rendered in the accompanying way:

@for(error

Default values while Filling Form 

Here and there you’ll need to fill a form with existing qualities, ordinarily for altering:

userform = userform.fill(new User(“rits@gmail.com”));

Form articles are changeless – calls to systems like bind () and fill () will give back another article loaded with the new data.

Handling a form that is not identified with a Model

You can utilize a Dynamicform on the off chance that you have to recover data from a html form that is not identified with a Model:

public static Result hi() {
Dynamicform requestdata = Form.bindfromrequest();
String firstname = requestdata.get("firstname");
String lastname = requestdata.get("lastname");
return ok("hello " + firstname + " + lastname);
}

Register a custom Databinder

On the off chance that you need to characterize a mapping from a custom article to a form field string and the other way around you have to enlist another Formatter for this item.

For an item like Jodatime’s Localtime it could resemble this:

Formatters.register(localtime.class, new Simpleformatter() 
{
private Pattern timepattern = Pattern.compile(
"([012]? \\d)(?:[\\s:\\._\\-]+([0-5]\\d))?"
);

@override
public Localtime parse(string data, Locale l) tosses Parseexception {
Matcher m = timepattern.matcher(input);
In the event that (!m.find()) toss new Parseexception("No Input");
int hour = Integer.valueof(m.group(1));
int min = m.group(2) == invalid ? 0 : Integer.valueof(m.group(2));
return new Localtime(hour, min);
}
@override
public String print(localtime, Locale) {
return localtime.tostring("hh:mm");
}
});

At the point when the coupling fall flat an exhibit of failures keys is made, the first characterized in the messages document will be utilized. This cluster will for the most part hold :

["error.invalid.<fieldname>", "error.invalid.<type>", "error.invalid"]