myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Myfaces Wiki] Update of "Extensions/Validator/Getting_Started/Cross_Validation" by GerhardPetracek
Date Sun, 22 Nov 2009 19:08:35 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Myfaces Wiki" for change notification.

The "Extensions/Validator/Getting_Started/Cross_Validation" page has been changed by GerhardPetracek.
http://wiki.apache.org/myfaces/Extensions/Validator/Getting_Started/Cross_Validation?action=diff&rev1=2&rev2=3

--------------------------------------------------

  
  ... override these methods to display a meaningful reverse validation message at the source
component.
  
+ == Conditional validation via cross-validation ==
+ 
+ Conditional validation is possible e.g. via @SkipValidation - that works if the condition
is already available before the current request.<<BR>>
+ It might be interesting to validate constraints based on a condition which is available
with the same request.<<BR>>
+ The group validation concept available via BV as well as via a possible add-on for ExtVal-Constraints
(a simple example is available at [[http://code.google.com/p/os890/source/browse/#svn/trunk/java/web/jsf/extval/examples/advanced/demo_112|demo
of group validation light]]) might already solve your requirements.
+ 
+ If you prefer a style which directly uses JSF mechanisms, you can also use cross-validation
for it. The validation target can be used as condition.
+ 
+ It's possible since the first version of ExtVal. Due to new features introduced in the 3rd
release of ExtVal you will see additional things in the example you might not have seen so
far. However, they aren't required since the approach just uses the mechanism of cross-validation.:
+ 
+ {{{
+ @Target({METHOD, FIELD})
+ @Retention(RUNTIME)
+ @Documented
+ @UsageInformation(UsageCategory.API)
+ public @interface ValidateLengthIf
+ {
+     String[] validateIf();
+ 
+     int minimum() default 0;
+ 
+     int maximum() default Integer.MAX_VALUE;
+ 
+     Class<? extends ValidationParameter>[] parameters() default ViolationSeverity.Error.class;
+ }
+ }}}
+ 
+ {{{
+ @SkipValidationSupport
+ public class ValidateLengthIfValidationStrategy extends AbstractCompareStrategy<ValidateLengthIf>
+ {
+     private UIComponent component;
+     private FacesMessage facesMessage;
+ 
+     @Override
+     protected void initCrossValidation(CrossValidationStorageEntry crossValidationStorageEntry)
+     {
+         this.component = crossValidationStorageEntry.getComponent();
+     }
+ 
+     public boolean isViolation(Object source, Object target, ValidateLengthIf annotation)
+     {
+         if (Boolean.TRUE.equals(target))
+         {
+             try
+             {
+                 LengthValidator lengthValidator = resolveLengthValidator();
+                 lengthValidator.setMinimum(annotation.minimum());
+                 lengthValidator.setMaximum(annotation.maximum());
+                 lengthValidator.validate(FacesContext.getCurrentInstance(), this.component,
source);
+             }
+             catch (ValidatorException e)
+             {
+                 this.facesMessage = e.getFacesMessage();
+                 return true;
+             }
+         }
+         return false;
+     }
+ 
+     private LengthValidator resolveLengthValidator()
+     {
+         return (LengthValidator)FacesContext.getCurrentInstance()
+                 .getApplication().createValidator("javax.faces.Length");
+     }
+ 
+     public String[] getValidationTargets(ValidateLengthIf annotation)
+     {
+         return annotation.validateIf();
+     }
+ 
+     @Override
+     public boolean useTargetComponentToDisplayErrorMsg(CrossValidationStorageEntry crossValidationStorageEntry)
+     {
+         return false;
+     }
+ 
+     @Override
+     protected String getErrorMessageSummary(ValidateLengthIf annotation, boolean isTargetComponent)
+     {
+         return this.facesMessage.getSummary();
+     }
+ 
+     @Override
+     protected String getErrorMessageDetail(ValidateLengthIf annotation, boolean isTargetComponent)
+     {
+         return this.facesMessage.getDetail();
+     }
+ 
+     protected String getValidationErrorMsgKey(ValidateLengthIf annotation, boolean isTargetComponent)
+     {
+         //for using the message of the std. validator instead of a cross-validation-key
for extval message resolving
+         return null;
+     }
+ }
+ }}}
+ 

Mime
View raw message