Dear Wiki user, You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.
The following page has been changed by SebastianHennebrueder: http://wiki.apache.org/tapestry/CreatingCustomValidators ------------------------------------------------------------------------------ = Creating a custom validator = - applies to: Tapestry 4.0 + Explains how to write and integrate custom validators for Tapestry 4 and 5 + + == Tapestry 5.0 == + Author: Sebastian Hennebrueder, Date: August 2009 + There are 3 steps: + 1. Create a validator + 1. Create a resource file for validation messages + 1. Register both in your application + + === Create a validator === + The following validator check if the value is not 'foo' because foo is really a bad value. It + expects a String and no configuration parameter. The {{{MinLength}}} validator uses a configuration parameter to define the minimum length. + The validation error messages has the key {{{foo-not-allowed}}}. You might check the source code of the existing validators for further inspiration. + {{{ + import org.apache.tapestry5.validator.AbstractValidator; + // ... some more imports here + public class FooValidator extends AbstractValidator<Void, String> { + public FooValidator() { + super(null, String.class, "foo-not-allowed"); + } + + public void validate(Field field, Void constraintValue, MessageFormatter formatter, String value) + throws ValidationException { + if ("foo". equals(value)) + throw new ValidationException(buildMessage(formatter, field, constraintValue)); + } + + private String buildMessage(MessageFormatter formatter, Field field, Void constraintValue) { + return formatter.format(constraintValue, field.getLabel()); + } + + public void render(Field field, Void constraintValue, MessageFormatter formatter, MarkupWriter writer, + FormSupport formSupport) { + formSupport.addValidation(field, "foo", buildMessage(formatter, field, constraintValue), null); + } + } + }}} + === Create a resource file for validation messages === + File ValidationMessages.properties + {{{ + foo-not-allowed=Foo is not allowed + }}} + + === Register both in your application === + A Tapestry application is configured by a AppModule class (See the Tapestry Guide). + In the {{{AppModule}}} class add the following methods: + {{{ + public static void contributeValidationMessagesSource(OrderedConfiguration<String> configuration) { + configuration.add("Default", "de/laliluna/example/components/ValidationMessages", "before:*"); + } + + public static void contributeFieldValidatorSource(MappedConfiguration<String, Validator> configuration) { + configuration.add("foo", new FooValidator()); + } + }}} + That's it. + + == Tapestry 4.0 == this is a revised copy of a posing to the users mailing list by Scott F. Walter == 1. implementing a validator == --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
