abhig
abhig

Reputation: 860

how to show errors of validate() in Play Java

I am using play 2.2.0 for java

public String email;
public String validate(){

    if(email!= null &&(email.endsWith("@xyz.com") )){
        return null;
    }
    System.out.println("error");
    return "Please Add valid xyz  email id only ";
}

above is my code for validation of email

my question is how to show "Please Add valid xyz email id only" in the form if user error a wrong email id

I tried

@helper.inputText(playerForm("email"), '_label -> "Email",'_help -> "Must be xyz id",   
    '_showConstraints -> false,
    '_showErrors -> true,
    '_error -> playerForm.error("email")
)

but it is not working

Upvotes: 0

Views: 780

Answers (2)

estmatic
estmatic

Reputation: 3449

Just to offer another solution, there are three versions of the validate() method you can use depending on your needs. You're using the simplest in which you simply return 'NULL' or a 'String' error message. The other two versions allow you to specify multiple errors on specific fields (so they don't go to the "global" error map.

public List<ValidationError> validate(){
  ...
}

public Map<String, List<ValidationError>> validate(){
  ...
}

As with the String version, you simply return NULL if there are no errors. If you do have errors then you populate a List or Map with one or more errors. The map version allows you to specify errors on multiple fields.

new ValidationError("email", "Please add valid xyz email id only.");

Upvotes: 1

abhig
abhig

Reputation: 860

Got the answer. It goes to global error you can get it through

@if(form.hasGlobalErrors) {
   <div class="alert alert-error">@form.globalError.message</div>
}

Upvotes: 0

Related Questions