Kontakt

Validation Macros

Posted on Mittwoch, 8th September, 2010

Upgrading my current applications to Tapestry 5.2 is such a pleasure. I can throw away pieces of code that annoyed me in the past. Let me give you an example. Did you ever wish a simple-minded way of combining several related validator constraints together under a single name? If so, you will love the new feature called validator macros as I do.

Imagine you implemented a page which is used by your end users to change their passwords. The page contains a form with two password fields. It is obvious that both fields need to be validated in exactly the same way, thus you need to define your validation constraints twice as shown in the following listing (ChangePassword.tml).

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
   <form t:type="form">
      <t:errors/>
      <p>
         New Password:
         <input t:type="PasswordField" t:id="password"
                validate="required,minlength=10,maxlength=50"/>
      </p>
      <p>
         Repeat Password:
         <input t:type="PasswordField" t:id="passwordRepeated"
                validate="required,minlength=10,maxlength=50"/>
      </p> 

      <p>
         <input type="submit" value="Change It"/>
      </p>
    </form>
</html>

The page class is shown in the following listing:

public class ChangePassword {

   @Property
   private String password;

   @Property
   private String passwordRepeated;

   void onValidateForm() { ... }

   void onSuccess() { ... }
}

Needles to say, repeating some code is very ugly and should be avoided. As of Tapestry 5.2 you can define new validators by combining several related validators together under a single name. The following listing shows how to accomplish this. You just contribute to the configuration of the ValidatorMacro service that is involved into the validation mechanism. The service’s configuration maps a validation macro name to a comma-separated list of validation constraints. When validating form fields Tapestry will ask this service to resolve a macro name (a simple string) to a set of validation constraint.

public class AppModule {

   @Contribute(ValidatorMacro.class)
   public static void combineValidators(
      MappedConfiguration<String, String> configuration) {
      configuration.add(
            "password",
            "required,minlength=10,maxlength=50");
   }
}

Once your made the contribution, it is possible to pass the password validation macro to the validate parameter of the form fields. This way you can avoid the redundancy of validation constraints for both password fields. See the following listing:

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
   <form t:type="form">
      <t:errors/>
      <p>
         New Password:
         <input t:type="PasswordField" t:id="password"
                validate="password"/>
      </p>
      <p>
         Repeat Password:
         <input t:type="PasswordField" t:id="passwordRepeated"
                validate="password"/>
      </p> 

      <p>
         <input type="submit" value="Change It"/>
      </p>
    </form>
</html>

Cool, isn’t? Tapestry 5.2 has a lot of improvements and I can’t await to see the final release.

 

avatar

Your name

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Tapestry 5 Blog - Copyright © 2009 - Eclectic Theme by Your Inspiration Web - Powered by WordPress