Reputation: 8067
I am using Play Framework 1.2.5 with Java. I have a login form. I am trying to validate the login form with an required check i.e. both the username and password are required. I did this code:
public static void welcome(@Required String txtUserName, @Required String txtPassword){
if(Validation.hasErrors()){
flash.error("Oops!!! Please enter your credentials!");
login();
}
render(txtUserName,txtPassword);
}
For displaying the messages, this is what I have in the heml page:
<td align="right">
#{if flash.error}
<p style="color: red;">
${ flash.error }
</p>
#{/if}
</td>
If any one or both of them is absent then I want to display specific messages i.e. If User Name is absent then the message should be "Please provide a User Name!!!". Presently, it is displaying a common message.
Please let me know how to achieve this.
Upvotes: 0
Views: 146
Reputation: 1978
The Validation class has a method hasError:
/**
* @param field The field name
* @return True is there are errors related to this field
*/
public static boolean hasError(String field) {
return error(field) != null;
}
Try to use this method like this:
if(validation.hasError("name_of_username_form_field")){
flash.error("Username required");
login();
}else if (validation.hasError("name_of_password_form_field")){
flash.error("Password required");
login();
}
The field name must match the name of the one of the input element in your groovy template.
Good luck!
Upvotes: 1