...
The template for a page contains a minimal amount of Tapestry instrumentation:
Code Block | ||||
---|---|---|---|---|
| ||||
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"> <head> <title>Login</title> </head> <body> <h1>Please Login</h1> <form t:type="form" t:id="form"> <t:errors/> <t:label for="userName"/>: <input t:type="TextField" t:id="userName" t:validate="required,minlength=3" size="30"/> <br/> <t:label for="password"/>: <input t:type="PasswordField" t:id="password" t:validate="required,minlength=3" size="30"/> <br/> <input type="submit" value="Login"/> </form> </body> </html> |
...
For example, your template may have the following:
Code Block | ||||
---|---|---|---|---|
| ||||
<t:textfield t:id="ssn" validate="required,regexp"/> |
...
Validator | Constraint Type | Description | Example |
Ensures that the given input is a valid e-mail address | | ||
max | long | Enforces a maximum integer value | |
maxLength | int | Makes sure that a string value has a maximum length | |
min | long | Enforces a minimum integer value | |
minLength | int | Makes sure that a string value has a minimum length | |
regexp | pattern | Makes sure that a string value conforms to a given pattern | |
required | Makes sure that a string value is not null and not the empty string | |
Validation Macros
Since Tapestry 5.2, we can create validation macros, wich will contain a list of validators. This mechanism is very useful for combining your validators. We just have to contribute to the ValidatorMacro Service in your AppModule, by adding a new entry to the configuration object. The first parameter is the name of your macro, the second is a comma-separated list of validators.
Code Block | ||||
---|---|---|---|---|
| ||||
@Contribute(ValidatorMacro.class)
public static void combinePasswordValidators(MappedConfiguration<String, String> configuration) {
configuration.add("password","required,minlength=5,maxlength=15,");
}
|
Then, we can use this new macro in our Template or Java Class.
Code Block | ||||
---|---|---|---|---|
| ||||
<input t:type="textField" t:id="password" t:validate="password" />
|
Code Block | ||||
---|---|---|---|---|
| ||||
@Validate("password")
private String password;
|
Overriding the Translator with Events
...