DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=14146>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=14146

Support for preconditions

           Summary: Support for preconditions
           Product: Commons
           Version: unspecified
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: Validator
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]


To keep validators independent of each other there is a need for the validator 
framework to support conditionally invoking validators.

This would avoid date validator having to check for the presence of null or 
empty value to allow for optional date fields and also serve conditional 
validating a field depending on the value of another field.

Prior to refactoring the validator code, I was able to use validator 
dependencies to check for some of these preconditions like daterange validator 
is not invoked until all dates are valid which allowed me to use a field 
like "fromDate" as a minimum value when running daterange on "toDate" field. 
This precondition will now allow me to achieve the same and at the same time 
allow daterange to be run on other fields.

This can be implemented by allowing a precondition tag; here is an example

To run the required validator for the bankId only if bankIdType has a value. 
<field property="bankId" depends="required,routingNumber" >
    <precondition name="required" property="bankIdType" valueExists="true"/> 
    <precondition name="routingNumber" property="bankIdType" valueExists="true" 
value="ABA"/>
</field>

I have extended the validator, Field, ResourceInitializer classes and added a 
new class Precondition.

In Validation Resource Initializer

 // Create Precondition objects
 digester.addObjectCreate("form-validation/formset/form/field/precondition",
                              "com.s1.arch.ui.vfx.Precondition",
                              "className");
 digester.addSetProperties("form-validation/formset/form/field/precondition");
 digester.addSetNext("form-validation/formset/form/field/precondition",
                        "addPrecondition", "com.s1.arch.ui.vfx.Precondition");

In my Valdiator :

validateFieldForRule(...............
{
  if (! runValidator(getResource(BEAN_KEY), results, va,  field))
        return true;

      return super.validateFieldForRule(field, va, results, hActions, pos);

    }

    /**
      * Verify if the validator needs to be run. Returns true if  preconditions 
are satisfied.
      *
      * <precondition name="required" property="oneTimePmt" valueExists="true"/>
      * will run the required validator for the field only if oneTimePmt has
      * a value. You can specify the value of the property eg : <precondition
      * name="required" property="oneTimePmt" valueExists="true" value="false"/>
      */
      protected static boolean runValidator(Object bean, ValidatorResults 
results,
                                                        ValidatorAction va, 
Field field) {

         // verify if the validation is conditional
         Map resultMap = results.getResultValueMap();
         Collection  conditions = field.getPreconditions();
         Iterator itr = conditions.iterator();
         Precondition condition = null;
         while(itr.hasNext()) {
            condition = (Precondition)itr.next();
            if (condition.getName().equals(va.getName())) {

               String expectedValue = condition.getValue();
   String fieldValue = null;
               
  // get the transformed value from the valuemap if any.            
               if (results.getValidatorResult(condition.getProperty()) != null)
                  fieldValue = VFXUtil.getValueAsString(
                                        resultMap.get(condition.getProperty()));

               else // use the user entered value
                   fieldValue = ValidatorUtil.getValueAsString(
                                              bean, condition.getProperty());

               // validator should be run if the value exists
               if(GenericValidator.isBlankOrNull(fieldValue)) {
                 if (condition.getValueExists())
                    return false;
               }
               // validator should be run if the value does not exist
               else if (!condition.getValueExists())
                  return false;
               // validator should be run if the expected value(if any)
               // matches the property value.
               else if(!GenericValidator.isBlankOrNull(expectedValue) &&
                        !fieldValue.equals(expectedValue))
                  return false;
            }

         }
         return true;

      }

In  Field ..

 /**
     * Add a <code>Precondition</code> to the <code>Field</code>.
    */
    public void addPrecondition(Precondition c) {
       if (c != null && c.getName() != null && c.getName().length() > 0 && 
c.getProperty() != null) {
          hPreconditions.put(c.getName() + c.getProperty(), c);
       }
    }

    /**
     * Retrieve all precondition.
    */
    public Collection getPreconditions() {
       return hPreconditions.values();
    }


Precondition class :

public class Precondition implements Cloneable, Serializable {

    // name of the validator
    protected String name = null;
    protected String property = null;
    protected String value = null;
    protected boolean valueExists = false;
    /**
     * Gets the validator name.
    */
    public String getName() {
       return name;
    }

    /**
     * Sets the validator name.
    */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Gets the property name of the field.
    */
    public String getProperty() {
       return property;
    }


    /**
     * Sets the property name of the field.
    */
    public void setProperty(String property) {
      if (property != null)
        this.property = property.trim();
    }

    /**
     * Gets the condition value.
    */
    public String getValue() {
       return value;
    }


    /**
     * Sets the condition value
    */
    public void setValue(String value) {
        this.value = value;
    }

    /**
     * condition is to check if the field exists
     */
     public boolean getValueExists() {
      return valueExists;
     }
     public void setValueExists(boolean exists) {
      this.valueExists = exists;
     }

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@;jakarta.apache.org>

Reply via email to