Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The template for a page contains a minimal amount of Tapestry instrumentation:

Code Block
javahtmljava
html
<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
javahtmljava
html
  <t:textfield t:id="ssn" validate="required,regexp"/>

...

Validator

Constraint Type

Description

Example

email

Ensures that the given input is a valid e-mail address

<t:textfield value="email" validate="email" />

max

long

Enforces a maximum integer value

<t:textfield value="age" validate="max=120,min=0" />

maxLength

int

Makes sure that a string value has a maximum length

<t:textfield value="zip" validate="maxlength=7" />

min

long

Enforces a minimum integer value

<t:textfield value="age" validate="max=120,min=0" />

minLength

int

Makes sure that a string value has a minimum length

<t:textfield value="somefield" validate="minlength=1" />

regexp

pattern

Makes sure that a string value conforms to a given pattern

<t:textfield value="otherfield" validate="regexp=^a-z+$" />

required

Makes sure that a string value is not null and not the empty string

<t:textfield value="name" validate="required" />

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
java
java

@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
html
html

<input t:type="textField" t:id="password" t:validate="password" />
Code Block
java
java

@Validate("password")
private String password;

Overriding the Translator with Events

...