Goal and current statusGoal of project is support other validation frameworks(hibernate-validator, JSR-303) in tapestry 5.
Currently supports hibernate-validator 3.x project.
Hibernate validator supportAllows you to use hibernate validator annotations for validation in tapestry web layer.
Support includes
Validation for annotated property of bean, which is binding with a form field(t:textfield, t:textarea, etc...) on client(with javascript) and server side. Validation for annotated properties of bean in beanEditForm field on client(with javascript) and server side. Allows you to use the hibernate validator meassage engine with tapestry messages Includes support for your custom hibernate validators
Enable hibernate validator in tapestryIn order to enable validation needs to include the module com.googlecode.tapestry5validator.hibernate.TapestryHibernateValidatorModule
import com.googlecode.tapestry5validator.hibernate.TapestryHibernateValidatorModule;
@SubModule(TapestryHibernateValidatorModule.class)
public class AppModule {
}Validation for binding propertyCreate a simple pojo class
public class Submarine {
@Max(10)
private int height;
@Max(value=11, message="Width must be less {value}")
private int width;
@Max(value=12, message="{submarine.gravity.max}")
private int gravity;
@Max(value=13, message="Overriden in view")
private int cost;
@Max(value=14, message="{missing.key}")
private int count;
//getters and setters...
}Create page submarine
Java:
public class View {
@Property
private Submarine submarine = new Submarine();
}tml:
Tapestry Hibernate Validator Test
Submarine demo
Run your application. Fill in the field height value greater than 10.
If you have enabled javascript, it will work on the client side validation
If you have disabled javascript, the result will be as follows
Validation for BeanEditorFormJava:
public class Messages {
@Property
private Submarine submarine;
}tml:
Tapestry Hibernate Validator Test
Submarine
Client side validation:
Server side validation:
Messages and localizationYou can use hibernate validator message engine(interpolation) with tapestry messages engine. Algorithm to search for messages
In tapestry messages. Supported hibernate interpolation. In classpath:ValidatorMessages.properties file In Default hibernate messages Hibernate messagesCreate in classpath file ValidatorMessages_en.properties.
submarine.gravity.max=must be less {value}@Max(value=12, message="{submarine.gravity.max}")
private int gravity; If the message begins with a lowercase letter, the beginning of the line will be added to the name field.
Result:
Gravity must be less 12Tapestry messages You can override hibernate messages through the tapestry messages, using the standard tapestry message engine
formId + "-" + fieldId + "-" + validatorName + "-message" fieldId + "-" + validatorName + "-message" where validatorName - hibernate validator name. All hibernate validators have names in the format
hv-{hibernateValidatorAnnotationName}examples:
hv-max
hv-patternTapestry messages supported hibernate interpolation. Also you can format field name in tapestry messages.
example:
@Max(value=10)
private int cost;cost-hv-max-message=%s must be less {value}Where:
cost - Field name in template hv-max - Validator name %s - Formated to field name, if need {value} - Interpolated parameter name in annotation(value) Result:
Cost must be less 10Add your custom validatorWrite custom validator. Write tapestry adapter, implementing interface com.googlecode.tapestry5validator.hibernate.ValidatorAdapter Create javascript validation method Tapestry.Validator.hvEmail = function(field, message) {
field.addValidator(function(value) {
if (!Tapestry.Validator.isRFC822ValidEmail(value)) {
throw message;
}
});
};Contribute HibernateValidatorsProvider
public static void contributeSupportedHibernateValidatorsProvider(
OrderedConfiguration configuration) {
addToConfigation(new CustomAdapter(), configuration);
}
Restrictions@AssertTrue, @AssertFalse - Not supported validation all boolean fields. Tapestry does not provide opportunities to validate boolean fields @Future and @Past - Validation only on server side. Tapestry encode / decode the date from a string to the server in the appropriate locale. As JavaScript is not enough for this information. You can not trust with a value date of the client. @NotEmpty @Size - Because it's for collections @Pattern Supported only flags java.util.regex.Pattern.MULTILINE, java.util.regex.Pattern.CASE_INSENSITIVE @Patterns - In future
Add to your project, using mavenAdd repository definition to pom.xml
lightstack
Lihgtstack project repository
http://lightstack.googlecode.com/svn/maven/repository
And add dependency
com.googlecode.tapestry5validator
tapestry-validator-hibernate
0.0.1-SNAPSHOT
30 Day Summary May 17 2013 — Jun 16 2013
|
12 Month Summary Jun 16 2012 — Jun 16 2013
|
Copyright
©
2013
Black Duck Software, Inc.
and its contributors, Some Rights Reserved. Unless otherwise marked, this work is licensed under a
Creative Commons Attribution 3.0 Unported License
. Ohloh
®
and the Ohloh logo are trademarks of
Black Duck Software, Inc.
in the United States and/or other jurisdictions. All other trademarks are the property of their respective holders.