[CONF] Apache Tapestry > Bean Validation

2014-01-18 Thread Bob Harner (Confluence)














  


Bob Harner edited the page:
 


Bean Validation   




 Comment: fixed language param of code macro 





since








since
5.2


 




 
 
 






 Wiki Markup




 {float:right|background=""
{contentbylabel:title=Related Articles|showLabels=false|showSpace=false|space=TAPESTRY|labels=validation}
{float} 



...
The BeanValidatorSource service 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.



 Code Block








language
java
 

[CONF] Apache Tapestry > Bean Validation

2011-03-08 Thread confluence







Bean Validation
Page edited by Igor Drobiazko


 Changes (10)
 




...
h1. Integrate JSR 303 - Bean Validation API  
Tapestry provides a powerful validation mechanism which is described [here|Input Validation]. Among other things this mechanism allows you to annotate your domain model classes with the annotation [@Validate|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/beaneditor/Validate.html] annotation. 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_ module. To make your domain model independent from Tapestry you can use the [JSR 303: Bean Validation|http://jcp.org/en/jsr/detail?id=303]. This library provides integration between Tapestry and JSR-303. 
 h2. Configuration  
The Tapestry's JSR 303 - Bean Validation Library is responsible for configuring and bootstrapping the [Validator|http://download.oracle.com/javaee/6/api/javax/validation/Validator.html] for you. In order to use this library you have to choose an implementation of the JSR-303 specification like [Hibernate Validator 4.x|https://www.hibernate.org/412.html]. This library is not specific to any implementation of JSR-303 and will work with any implementation of your choice. 
 h3. Bootstraping the Bean Validator  
The service [BeanValidatorSource|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/beanvalidator/BeanValidatorSource.html] service is responsible for bootstrapping the [Validator|http://download.oracle.com/javaee/6/api/javax/validation/Validator.html]. You can contribute a [BeanValidatorConfigurer|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/beanvalidator/BeanValidatorConfigurer.html] to the configuration of this service in order to participate on the configuration of [Validator|http://download.oracle.com/javaee/6/api/javax/validation/Validator.html]. 
 {code:java} @Contribute(BeanValidatorSource.class) 
public static void provideBeanValidatorConfigurer(OrderedConfiguration configuration) 
{ 
   configuration.add("MyConfigurer", new BeanValidatorConfigurer() 
   { 
  public void configure(javax.validation.Configuration configuration) 
  {  configuration.ignoreXmlConfiguration(); 
...
h3. 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|http://download.oracle.com/javaee/6/api/javax/validation/groups/Default.html] group is taken. By default, Tapestry passes only this group to [Validator|http://download.oracle.com/javaee/6/api/javax/validation/Validator.html]. You can tell Tapestry to pass more groups by contributing group classes into the configuration of the [BeanValidatorSource|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/beanvalidator/BeanValidatorSource.html] service. 
 h2. Usage 
...
   private String userName;  
   @NotNull 
   @Size(min=5, max=30)@Property @Persist 
...
}{code}  
You can event mix JSR-303 annotations and Tapestry's @[Validate|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/beaneditor/Validate.html] annotation. 
 {code:java} 
...
   private String userName;  
   @NotNull 
   @Validate("minlength=5,maxlength=30")@Property @Persist 
...
}{code}  
Next you have to pass the object to validate into the Form's parameter _validate_ parameter. In the following example the Form's fields are bound to the properties of the page _Login_ page. That's why we pass _this_, thus the page instance, into the parameter _validate_. to the  _validate_ parameter. 
 {code:xml} 
...
   
 
 
 
   
...
{code}  
Since the parameter _validate_ parameter defaults to the container of the Form component, we could also remove _validate="this"_ in the example above. 
 h3. Validating Beans with BeanEditForm 
...
   private String userName;  
   @NotNull 
   @Validate("minlength=10")private String password; 
...
h3. Client-side Validation  
Unfortunately JSR-303 doesn’t cover the client-side validation, so web frameworks supporting this JSR must provided still proprieta

[CONF] Apache Tapestry > Bean Validation

2010-12-21 Thread confluence







Bean Validation
Page edited by Igor Drobiazko


 Changes (1)
 



...
 h3. Client-side Validation 
 Unfortunately JSR-303 doesn’t cover the client-side validation, so that most web frameworks supporting this JSR have to provide proprietary solutions. Tapestry provides client-side validation for following JSR-303 constraints:  |JSR-303 constraint |Tapestry' _javascript_ function| | [...@max|http://download.oracle.com/javaee/6/api/javax/validation/constraints/Max.html] | Tapestry.Validator.maxnumber | | [...@min|http://download.oracle.com/javaee/6/api/javax/validation/constraints/Min.html] | Tapestry.Validator.minnumber | | [...@notnull|http://download.oracle.com/javaee/6/api/javax/validation/constraints/NotNull.html] | Tapestry.Validator.notnull | | [...@null|http://download.oracle.com/javaee/6/api/javax/validation/constraints/Null.html] | Tapestry.Validator.isnull | | [...@pattern|http://download.oracle.com/javaee/6/api/javax/validation/constraints/Pattern.html] | Tapestry.Validator.pattern | | [...@size|http://download.oracle.com/javaee/6/api/javax/validation/constraints/Size.html] | Tapestry.Validator.size |   h3. Providing own client-side validators  Now let's see how to provide own client-side validation for JSR-303 constraints. Imagine you created the following constraint definition. The server-side implementation of the constraint is implemented by RangeValidator. I suppose you are familiar with JSR-303, so I don’t explain how to implement RangeValidator.   {code:java} @Documented @Constraint(validatedBy = RangeValidator.class) @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) public @interface Range {long max() default Long.MAX_VALUE; long min() default Long.MIN_VALUE; String message() default "{com.acme.constraint.Range.message}";  Class[] groups() default {};  Class[] payload() default {}; }{code}  To provide client-side validation of a constraint you have to add a _javascript_ function to the built-in object _Tapestry.Validator_. The function should contain exactly three parameters:  # Field being validated # Validation message # JSON object with values from the constraint annotation  Here is an example:  {code:_javascript_} Tapestry.Validator.range = function(field, message, spec) {field.addValidator(function(value) {   if (value < spec.min || value > spec.max) {  throw message;   }}); };{code}  Now you have to tell Tapestry to call the function _Tapestry.Validator.range_ when client-side validation of _...@range_ should be executed. This is accomplished by a contribution to the service [ClientConstraintDescriptorSource|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/beanvalidator/ClientConstraintDescriptorSource.html]. The configuration of this service is a collection of [ClientConstraintDescriptor|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/beanvalidator/ClientConstraintDescriptor.html]. Each _ClientConstraintDescriptor_ represents a client-side validation constraint. The constructor of _ClientConstraintDescriptor_ has three parameters:  # Class of the constraint annotation. # Name of the _javascript_ function. # The last parameter is a varargs. It is used to pass the attribute names of the constraint annotation to be passed (along with their values) to the _javascript_ function as an JSON object.  The last step is to make the contribution, which links the _...@range_ annotation with the _javascript_ function _range_. The attributes _max_ and _min_ and their values are passed to the function.  {code:java} @Contribute(ClientConstraintDescriptorSource.class) public static void provideClientConstraintDescriptors(Configuration config) { config.add(new ClientConstraintDescriptor(Range.class, "range", "min", "max")); } {code} 

Full Content



Added in 5.2


Related Articles


 
 Input Validation





 
 Bean Validation






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 provid

[CONF] Apache Tapestry > Bean Validation

2010-12-21 Thread confluence







Bean Validation
Page edited by Igor Drobiazko


 Changes (1)
 



{since:since=5.2} 
{float:right|background="" {contentbylabel:title=Related Articles|showLabels=false|showSpace=false|space=TAPESTRY|labels=validation} 
...

Full Content



Added in 5.2


Related Articles


 
 Input Validation





 
 Bean Validation






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



xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
   
  "this">

 

 
"userName"/>
 
 
 
"password"/>
 
 
 
"submit" value="Login"/>
 
  
   



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



Change Notification Preferences

View Online
|
View Changes









[CONF] Apache Tapestry > Bean Validation

2010-12-20 Thread confluence







Bean Validation
Page edited by Igor Drobiazko


 Changes (2)
 



...
h3. 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.  {code:java} public class Login {@NotNull@Size(max=10)@Pattern(regexp = "[a-zA-Z]*")@Property @Persistprivate String userName; @NotNull @Size(min=5, max=30)@Property @Persistprivate String password; void onSuccess(){   // Login the user here} }{code}  You can event mix JSR-303 annotations and Tapestry's [...@validate|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/beaneditor/Validate.html] annotation.  {code:java} public class Login {@NotNull@Validate("maxlength=10")@Pattern(regexp = "[a-zA-Z]*")@Property @Persistprivate String userName; @NotNull @Validate("minlength=5,maxlength=30")@Property @Persistprivate String password; void onSuccess(){   // Login the user here} }{code}  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_.  {code:xml} {code}  Since the parameter _validate_ defaults to the container of the Form component, we could also remove _validate="this"_ in the example above.  
h3. Validating Beans with BeanEditForm  
If you use the [BeanEditForm|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/corelib/components/BeanEditForm.html] 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.   {code:java} public class User {@NotNullprivate String userName; @NotNull @Validate("minlength=10")private String password; ... }{code}  
h3. Client-side Validation 

Full Content


Related Articles


 
 Bean Validation





 
 Input Validation






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

[CONF] Apache Tapestry > Bean Validation

2010-12-20 Thread confluence







Bean Validation
Page edited by Igor Drobiazko


 Changes (1)
 



{toc:type=list|maxLevel=3} 
{float:right} {info:title=Related Articles} [Input Validation] {info} {float} 
 h1. Integrate JSR 303 - Bean Validation API 
...

Full Content



Integrate JSR 303 - Bean Validation API

Configuration

Bootstraping the Bean Validator
Validation groups

Usage

Validating Input Fields
Validating Beans with BeanEditForm
Client-side Validation



Related ArticlesInput Validation

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

Validating Beans with BeanEditForm

Client-side Validation



Change Notification Preferences

View Online
|
View Changes









[CONF] Apache Tapestry > Bean Validation

2010-12-20 Thread confluence







Bean Validation
Page  added by Igor Drobiazko

 

 

Integrate JSR 303 - Bean Validation API

Configuration

Bootstraping the Bean Validator
Validation groups

Usage

Validating Input Fields
Validating Beans with BeanEditForm
Client-side Validation




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

Validating Beans with BeanEditForm

Client-side Validation


   
Change Notification Preferences
   
   View Online