You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Integrate JSR 303 - Bean Validation API

Tapestry provides a powerful validation mechanism which is described here. Among other things this mechanism allows you to annotate your domain model classes with the annotation @Validate. This annotation is problematic if your domain model is used in non-Tapestry applications as well as in Tapestry applications. Your non-Tapestry application becomes dependent on tapestry5-annotations. To make your domain model independent from Tapestry you can use the JSR 303: Bean Validation. This library provides integration between Tapestry and JSR-303.

Configuration

The Tapestry's JSR 303 - Bean Validation Library is responsible for configuring and bootstrapping the Validator for you. In order to use this library you have to choose an implementation of the JSR-303 specification like Hibernate Validator 4.x. This library is not specific to any implementation of JSR-303 and will work with any implementation of your choice.

Bootstraping the Bean Validator

The service BeanValidatorSource is responsible for bootstrapping the Validator. You can contribute a BeanValidatorConfigurer to the configuration of this service in order to participate on the configuration of Validator.

@Contribute(BeanValidatorSource.class)
public static void provideBeanValidatorConfigurer(OrderedConfiguration<BeanValidatorConfigurer> configuration) 
{
   configuration.add("MyConfigurer", new BeanValidatorConfigurer() 
   {
      public void configure(javax.validation.Configuration<?> configuration) 
      {
         configuration.ignoreXmlConfiguration();
      }
   });
}

Validation groups

In JSR-303 validation groups are used you to define a subset of the constraints validated at a given time. If no validation group is specified the default Defaul group is taken. By default Tapestry passes only this group to Validator. You can tell Tapestry to pass more groups by contributing group classes into the configuration of the BeanValidatorSource service.

Usage

Validating Input Fields

Once you included this library and its dependencies into your web app, you may use the JSR-303 annotations to validate the user's input.

public class Login
{
   @NotNull
   @Size(max=10)
   @Pattern(regexp = "[a-zA-Z]*")
   @Property @Persist
   private String userName;

   @NotNull 
   @Size(min=5, max=30)
   @Property @Persist
   private String password;

   void onSuccess()
   {
      // Login the user here
   }
}

You can event mix JSR-303 annotations and Tapestry's @Validate annotation.

public class Login
{
   @NotNull
   @Validate("maxlength=10")
   @Pattern(regexp = "[a-zA-Z]*")
   @Property @Persist
   private String userName;

   @NotNull 
   @Validate("minlength=5,maxlength=30")
   @Property @Persist
   private String password;

   void onSuccess()
   {
      // Login the user here
   }
}

Next you have to pass the object to validate into the Form's parameter validate. In the following example the Form's fields are bound to the properties of the page Login. That's why we pass this, thus the page instance, into the parameter validate.

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
   <body>
      <t:form validate="this">

         <t:errors/>

         <p>
            <t:textfield t:id="userName"/>
         </p>
         
         <p>
            <t:textfield t:id="password"/>
         </p>
         
         <p>
            <input type="submit" value="Login"/>
         </p>
      <t:form>
   </body>
</html>

Since the parameter validate defaults to the container of the Form component, we could also remove validate="this" in the example above.

Validating Beans with BeanEditForm

If you use the BeanEditForm component it's even easier to validate your beans. The only thing you have to do is to annotate your beans with JSR-303 annotations. If you are migrating from Tapestry's built-in validation mechanism to JSR-303 Bean Validation, you don't have to change your template at all.

public class User
{
   @NotNull
   private String userName;

   @NotNull 
   @Validate("minlength=10")
   private String password;

   ...
}

Client-side Validation

  • No labels