Easy Wicket & JSR 303 Integration Part I

It is very easy to integrate field/property based constraints with wicket. All that needs to be done is create your own validator as shown below. JSR 303 provides has with three types of constraint validators

  1. Field Based
  2. Property Based
  3. Class Based

package org.wicketstuff.adminpanel.validator;

import org.apache.wicket.validation.INullAcceptingValidator;
import org.apache.wicket.validation.IValidatable;

import javax.validation.Validator;
import javax.validation.ConstraintViolation;

import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.PropertyDescriptor;

import java.util.Set;

import org.wicketstuff.adminpanel.beans.BeanConfig;

/**
 * A property based bean validato
 */
public abstract class PropertyBeanValidator
   implements INullAcceptingValidator {

   private static final long serialVersionUID = 1L;
   private Class<strong> beanClass;</strong>
   private String propertyName;

   /**
    * Validates a bean property based on Bean Validator API
    * @param beanClass bean class to which property belongs to
    * @param propertyName property name
    */
   public PropertyBeanValidator(final Class<strong> beanClass,</strong>
      final String propertyName){
      this.beanClass = beanClass;
      this.propertyName = propertyName;
   }

   /**
    * Validate on null as the bean validator will take care of that
    * @return true
    */
   public final boolean validateOnNullValue(){
      return true;
   }

   /**
    * Validate validatable
    * @param validatable validatable
    */
   public void validate(final IValidatable validatable){
      final F value = validatable.getValue();
      final Set<strong>> violations = getBeanValidator().validateValue(</strong>
            beanClass, propertyName, value);
      for(final ConstraintViolation violation: violations){

         validatable.error(new DummyValidationError(violation.getMessage()));
      }

   }

   /**
    * Get bean validator
    * @return bean validator instance
    */
   public abstract Validator getBeanValidator();

   public PropertyDescriptor getPropertyDescriptor(){
      final PropertyDescriptor propertyDescriptor = getBeanValidator().
         getConstraintsForClass(beanClass).getConstraintsForProperty(propertyName);
      return propertyDescriptor;
   }

}

A better design would be to inject beanValidator using ioc. May be someday wicket with have its own @inject

Advertisements

Tagged:

3 thoughts on “Easy Wicket & JSR 303 Integration Part I

  1. Mreza January 24, 2012 at 2:09 PM Reply

    Part 2 ,3 ,… ????
    its good post but need more simple example and more description.
    if u can use a library except Zenika it will be great.

    • tawus January 25, 2012 at 12:42 PM Reply

      Before I could write Part 2, I saw tapestry đŸ™‚ and since then it is only tapestry.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: