HTML5 Validation
The HTML5 validation module provides a simple #{input /}
tag which can be used as a drop-in replacement for existing HTML <input>
elements. The #{input /}
tag will try to map existing data validation annotations from your Play! model to the HTML input element and thus provide near codeless client-side validation without even using JavaScript. On top of that it supports all available attributes from the original HTML input element and autofills the name
attribute by default.
Enable the HTML5 validation module
In the /conf/application.conf file, enable the HTML5 validation module by adding this line:
# The HTML5 validation module
module.html5validation=${play.path}/modules/html5validation-1.2
Usage
To use this module, simply replace those <input>
elements which need validation with the #{input /}
tag and specify the field from the corresponding model class using the for
attribute. All other attributes can just be copied from the existing <input>
element using the default Groovy syntax.
Example
Suppose you have a Model class called User
which has a field called name
declared as
@Required
@MaxSize(8)
@Match(“[a-z]*”)
public String name;
you can replace your existing <input>
element with the following:
#{input for:'user.name', id:'YourID', class:'class1 class2' /}
The tag will then output the following HTML code:
<input name="user.name" value="${user?.name}" id="YourID" class="class1 class2" required maxlength="8" pattern="[a-z]*">
Note: ${user?.name}
will actually be resolved to its current value.
Supported Annotations
The module currently supports the following annotations:
Issue tracking
If you want to report a bug or wish for a new feature, please use the issue tracker