date Range vialidation
laurentm Nov 21, 2008 9:46 PMHello fellow Seamers :)
I need to validate that a certain date type field is within secified range.
I made a custom Validator-based annotation but I can't seem to make it work correctly so any advices would be greatly appreciated.
FYI here are my validator Elements (code):
My annotation:
import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; import org.hibernate.validator.ValidatorClass; @ValidatorClass(DateRangeValidator.class) @Target({ METHOD, FIELD}) @Documented @Retention(RUNTIME) public @interface DateRange { String after() default ""; String before() default ""; String message() default ("validator.daterange"); }
The Validator itself
import java.io.Serializable; import java.util.Date; import org.hibernate.validator.Validator; import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope; @Name("daterangevalidator") @Scope(ScopeType.CONVERSATION) public class DateRangeValidator implements Validator<DateRange>, Serializable { /** * */ private static final long serialVersionUID = 7958814426292792622L; private long after=0l; private long before=0l; public void initialize(DateRange parameters) { after=new Long(parameters.after()).longValue(); before=new Long(parameters.before()).longValue(); System.out.println(after); } public boolean isValid(Object value ) { System.out.println(value); long lValue=((Date)value).getTime(); System.out.println(lValue); if(lValue<after) return false; if(lValue>before) return false; if((lValue>=after) &&(lValue<=before))return true; return false; } }
actually, the validator is never called
Thanks in advance for your help!
Laurent