On Tue, Mar 10, 2009 at 9:04 AM, taha siddiqi <[email protected]> wrote:
> But the problem is when you begin to use the wicket resource files, > hibernate-validator comes with its own messages and need to be > integrated with wicket ( which i was not able to ) There is some info here on how to pass a resource-bundle to the ClassValidator constructor, I haven't tried it though: http://www.jboss.org/index.html?module=bb&op=viewtopic&t=69783&view=next > > > taha > > On Tue, Mar 10, 2009 at 8:11 PM, Peter Thomas <[email protected]> wrote: > > On Tue, Mar 10, 2009 at 7:35 AM, taha siddiqi <[email protected]> > wrote: > > > >> Hi, > >> > >> I was working with Hibernate-validator and thought of sharing the code. > >> > > > > Taha: I thought the ClassValidator will handle all the different cases > and > > annotations for you, see line #89 > > > > > http://code.google.com/p/perfbench/source/browse/trunk/perfbench/wicket-jpa/src/main/java/wicketjpa/wicket/EditBorder.java#89 > > > > Is there any advantage of mapping each type (e.g. Min, Max, > > CreditCardNumber) to Wicket built-in validators ? > > > > > >> > >> It is a listener as well as a behavior which can be used at > >> application level as well as with individual > >> components. > >> > >> It is heavily inspired from wicket-jpa and wicket-hibernate > >> projects... I just tried some changes... > >> > >> import java.util.Map; > >> import java.util.Arrays; > >> import java.util.List; > >> import java.util.Date; > >> import java.util.concurrent.ConcurrentHashMap; > >> import javax.persistence.Entity; > >> import javax.persistence.Embeddable; > >> import java.lang.reflect.Field; > >> import java.lang.reflect.Method; > >> import java.lang.annotation.Annotation; > >> import java.lang.reflect.AnnotatedElement; > >> > >> import org.hibernate.validator.NotNull; > >> import org.hibernate.validator.Length; > >> import org.hibernate.validator.NotEmpty; > >> import org.hibernate.validator.Min; > >> import org.hibernate.validator.Pattern; > >> import org.hibernate.validator.Max; > >> import org.hibernate.validator.Range; > >> import org.hibernate.validator.Past; > >> import org.hibernate.validator.Future; > >> import org.hibernate.validator.Email; > >> import org.hibernate.validator.CreditCardNumber; > >> > >> import org.apache.wicket.validation.validator.StringValidator; > >> import org.apache.wicket.validation.validator.PatternValidator; > >> import org.apache.wicket.validation.validator.NumberValidator; > >> import org.apache.wicket.validation.validator.EmailAddressValidator; > >> import org.apache.wicket.validation.validator.CreditCardValidator; > >> import org.apache.wicket.validation.validator.DateValidator; > >> > >> import org.hibernate.validator.ClassValidator; > >> import org.hibernate.validator.InvalidValue; > >> import org.apache.wicket.behavior.AbstractBehavior; > >> import org.apache.wicket.application.IComponentOnBeforeRenderListener; > >> import org.apache.wicket.application.IComponentInstantiationListener; > >> import org.apache.wicket.Component; > >> import org.apache.wicket.markup.ComponentTag; > >> import org.apache.wicket.feedback.FeedbackMessage; > >> import org.apache.wicket.validation.IValidator; > >> import org.apache.wicket.validation.IValidatable; > >> import org.apache.wicket.model.IModel; > >> import org.apache.wicket.model.CompoundPropertyModel; > >> import org.apache.wicket.markup.html.form.FormComponent; > >> import org.apache.wicket.validation.ValidationError; > >> import org.apache.wicket.validation.validator.StringValidator; > >> import org.apache.wicket.AttributeModifier; > >> import org.apache.wicket.model.Model; > >> import org.slf4j.Logger; > >> import org.slf4j.LoggerFactory; > >> > >> /** > >> * A validator to validate elements having > >> * hibernate validators annotated. > >> */ > >> public class HibernateValidator extends AbstractBehavior > >> implements IComponentOnBeforeRenderListener { > >> > >> private static Logger logger = LoggerFactory.getLogger( > >> HibernateValidator.class ); > >> > >> public final void onBeforeRender( Component c ){ > >> if( !c.hasBeenRendered() ){ > >> configure(c); > >> } > >> } > >> > >> @Override > >> public void beforeRender( Component c ){ > >> super.beforeRender( c ); > >> configure( c ); > >> } > >> > >> @SuppressWarnings( "unchecked" ) > >> private boolean configure( Component c ){ > >> if( !isApplicableFor( c ) ){ > >> return false; > >> } > >> > >> FormComponent fc = (FormComponent)c; > >> CompoundPropertyModel cpm = > >> (CompoundPropertyModel)fc.getInnermostModel(); > >> > >> Class clazz = cpm.getObject().getClass(); > >> if( clazz.isAnnotationPresent( Entity.class ) || > >> clazz.isAnnotationPresent( Embeddable.class ) ){ > >> > >> try { > >> AnnotatedElement element = (AnnotatedElement) > >> > >> org.apache.wicket.util.lang.PropertyResolver.getPropertyGetter( > >> fc.getId(), cpm.getObject() ); > >> > >> addValidator( fc, element ); > >> }catch( Exception ex ){ > >> } > >> } > >> > >> return true; > >> } > >> > >> protected void addValidator( FormComponent fc, AnnotatedElement > element > >> ){ > >> //@NotNull > >> if( element.isAnnotationPresent( NotNull.class ) || > >> element.isAnnotationPresent( NotEmpty.class ) ){ > >> fc.setRequired( true ); > >> } > >> > >> //@Length( min, max ) > >> if( element.isAnnotationPresent( Length.class ) ){ > >> Length l = element.getAnnotation( Length.class ); > >> > >> if( l.min() == 0 && l.max() < Integer.MAX_VALUE ){ //TODO: > >> replace with max int > >> fc.add( StringValidator.maximumLength( l.max() ) ); > >> }else if( l.min() != 0 && l.max() < Integer.MAX_VALUE ){ > >> fc.add( StringValidator.lengthBetween( l.min(), l.max() ) ); > >> }else if( l.min() != 0 && l.max() >= Integer.MAX_VALUE ){ > >> fc.add( StringValidator.minimumLength( l.min() ) ); > >> } > >> } > >> > >> //@Max( value ) > >> if( element.isAnnotationPresent( Max.class ) ){ > >> Max max = element.getAnnotation( Max.class ); > >> fc.add( NumberValidator.maximum( max.value() ) ); > >> } > >> > >> //@Min( value ) > >> if( element.isAnnotationPresent( Min.class ) ){ > >> Min min = element.getAnnotation( Min.class ); > >> fc.add( NumberValidator.minimum( min.value() ) ); > >> } > >> > >> //@Range( min, max ) > >> if( element.isAnnotationPresent( Range.class ) ){ > >> Range range = element.getAnnotation( Range.class ); > >> fc.add( NumberValidator.range( range.min(), range.max() ) ); > >> } > >> > >> //@Pattern( regexp ) > >> if( element.isAnnotationPresent( Pattern.class ) ){ > >> Pattern pattern = element.getAnnotation( Pattern.class ); > >> fc.add( new PatternValidator( pattern.regex() ) ); > >> } > >> > >> //@Past > >> if( element.isAnnotationPresent( Past.class ) ){ > >> Past p = element.getAnnotation( Past.class ); > >> fc.add( DateValidator.maximum( new Date() ) ); > >> } > >> > >> //@Future > >> if( element.isAnnotationPresent( Future.class ) ){ > >> fc.add( DateValidator.minimum( new Date() ) ); > >> } > >> > >> //@Email > >> if( element.isAnnotationPresent( Email.class ) ){ > >> fc.add( EmailAddressValidator.getInstance() ); > >> } > >> > >> //@CreditCardNumber > >> if( element.isAnnotationPresent( CreditCardNumber.class ) ){ > >> fc.add( new CreditCardValidator() ); > >> } > >> } > >> > >> private boolean isApplicableFor( Component c ){ > >> if( ! (c instanceof FormComponent) ){ > >> return false; > >> } > >> > >> IModel model = c.getInnermostModel(); > >> if( model == null || model.getObject() == null || > >> !CompoundPropertyModel.class.isAssignableFrom( > model.getClass() > >> ) ){ > >> return false; > >> } > >> > >> return true; > >> } > >> } > >> > >> regards > >> Taha > >> > >> --------------------------------------------------------------------- > >> To unsubscribe, e-mail: [email protected] > >> For additional commands, e-mail: [email protected] > >> > >> > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > >
