I've got the validation working now.  Thought I'd share the
results and ask a question on style.  Both examples work, but as
a matter of best practice, which would be preferred?  Also which
would be more likely to remain in future cocoon releases?

The validation code has to call existing Java code for specific
validation methods.

Example 1:  Defining the validation in an <fd:javascript> child
of the field element, in form definition xml.

    <fd:field id="Number">
      <fd:datatype base="string"/>
      <fd:label>If your business or organisation has an ***
number, please show it here</fd:label>
      <fd:validation>
        <fd:javascript>
          var validator = new
Packages.online.common.model.Validation();
          var result = validator.validateNumber(widget.value);
          if (!result) { 
            widget.setValidationError(new
Packages.org.apache.cocoon.forms.validation.ValidationError("Some
Error!", false));
          }
          return result;
        </fd:javascript>
      </fd:validation>
    </fd:field>

Example 2: By wrapping the existing code as a WidgetValidator
and using the flow script to add to the field Element.  
  From form definition xml:

   <fd:field id="Number">
      <fd:datatype base="string"/>
      <fd:label>If your business or organisation has an ***
number, please show it here</fd:label>
    </fd:field>


  From flowscript (before showForm()):

    var validator = new
Packages.widgetValidators.NumberValidator();
    form.lookupWidget("Number").addValidator(validator);


 WidgetValidator Class:
    public class NumberValidator implements WidgetValidator {
      public boolean validate(Widget w) {
        boolean result = false;
        String val = w.getValue().toString();
        result = Validation.validateNumber(val);
        if(!result){
           ValidationError err = new ValidationError("Some
Error!",false);
           ((ValidationErrorAware)w).setValidationError(err);
        }
        return result;
      }
    }

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to