org.barracudamvc.core.forms
Class DefaultFormValidator

java.lang.Object
  extended by org.barracudamvc.core.forms.AbstractFormValidator
      extended by org.barracudamvc.core.forms.DefaultFormValidator
All Implemented Interfaces:
FormValidator
Direct Known Subclasses:
CardinalityValidator, DateRangeValidator, DateValidator, DigitValidator, EqualsValidator, LinkingValidator, MaxLengthValidator, MinLengthValidator, NotNullValidator, RangeValidator, RegexValidator, RegularExpressionValidator, SelectFormValidator, ValidatorListValidator, ValidTypeValidator

public class DefaultFormValidator
extends AbstractFormValidator

This class provides the default implementation of FormValidator.

A FormValidator is designed to validate one or more FormElements within a FormMap, OR a FormMap itself. The real work occurs in the validate() method. A validator is also capable of containing other validators, which makes it very easy to create a complex set of validation logic by assembling a "master" validator from a set of simpler validators.

When the validate method is invoked, we start by performing element level validation by invoking the validateFormElement() methods for each of the elements in the form. If that succeeds, we validate the whole map by invoking validateForm() method to validate the form itself. Finally, we invoke any child validators. In this way, a validator is not considered validated until all it's rules plus those for all of it's children have passed muster.

When an invalid condition occurs, typically the developer will throw a ValidationException, which stops the validation process immediately and returns the specified error. There are times, however, where the developer will want to try and validate as many of the rules as possible in order to return all known errors at once. In this case, the developer should throw DeferredValidationExceptions. These are caught internally and added to one master validation exception, which is thrown when the validation process completes.

In typical usage the developer would extend this class to override one of the three validateFormElement methods. FormValidators can also act as containers for other FormValidators, thereby allowing the developer to dynamically assemble complex validators from simpler validator objects.


Field Summary
protected static org.apache.log4j.Logger localLogger
           
protected  List validators
           
 
Constructor Summary
DefaultFormValidator()
          Public noargs constructor (errorMessage defaults to null)
DefaultFormValidator(String ierrorMessage)
          Public constructor with a default errorMessage to be dispatched when an invalid state occurs.
 
Method Summary
 void addValidator(FormValidator validator)
          Add a child validator
 List getValidators()
          Get a list of all child validators
 void removeValidator(FormValidator validator)
          Remove a child validator
 void validate(FormElement element, FormMap map, boolean deferExceptions)
          Validate a FormElement locally and allow any child validators a chance to validate as well.
 void validateForm(FormMap map, boolean deferExceptions)
          Validate an entire FormMap.
 void validateFormElement(Object val, FormElement element, boolean deferExceptions)
          Validate a FormElement.
 void validateFormElement(Object val, FormElement element, FormMap map, boolean deferExceptions)
          Validate a FormElement.
 
Methods inherited from class org.barracudamvc.core.forms.AbstractFormValidator
generateException, getErrorMessage, isNull, setErrorMessage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

localLogger

protected static final org.apache.log4j.Logger localLogger

validators

protected List validators
Constructor Detail

DefaultFormValidator

public DefaultFormValidator()
Public noargs constructor (errorMessage defaults to null)


DefaultFormValidator

public DefaultFormValidator(String ierrorMessage)
Public constructor with a default errorMessage to be dispatched when an invalid state occurs. The deferExceptions parameter defaults to true.

Parameters:
ierrorMessage - the default error message for this validator
Method Detail

addValidator

public void addValidator(FormValidator validator)
Add a child validator

Parameters:
validator - the child validator to be added

removeValidator

public void removeValidator(FormValidator validator)
Remove a child validator

Parameters:
validator - the child validator to be removed

getValidators

public List getValidators()
Get a list of all child validators

Returns:
a list of all child validators

validate

public void validate(FormElement element,
                     FormMap map,
                     boolean deferExceptions)
              throws ValidationException
Validate a FormElement locally and allow any child validators a chance to validate as well.

Parameters:
element - the form element to be validated (null indicates we want to perform form level validation)
map - the map to which the element belongs (sometimes necessary to validate elements by comparing them with other elements)
deferExceptions - do we want to deferValidation exceptions and attempt to validate all elements so that we can process all the exceptions at once
Throws:
ValidationException - if the element is not valid

validateForm

public void validateForm(FormMap map,
                         boolean deferExceptions)
                  throws ValidationException
Validate an entire FormMap. This is the method developers should override to provide specific validation for the entire form, as opposed to validating a specific element within the form. To indicate a form is invalid, through a ValidationException, which will interrupt the validation process immediately. If you want to indicate an error, but would still like validation to continue (so that you can identify multiple errors in one validation pass) throw a DeferredValidationException instead.

Parameters:
map - the map to be validated
deferExceptions - do we want to deferValidation exceptions and attempt to validate all elements so that we can process all the exceptions at once
Throws:
ValidationException - if the element is not valid

validateFormElement

public void validateFormElement(Object val,
                                FormElement element,
                                boolean deferExceptions)
                         throws ValidationException
Validate a FormElement. This is the method developers should override to provide specific validation based on the value and (potentially) information contained in the FormElement. To indicate an element is invalid, through a ValidationException, which will interrupt the validation process immediately. If you want to indicate an error, but would still like validation to continue (so that you can identify multiple errors in one validation pass) throw a DeferredValidationException instead.

Parameters:
val - the actual value to be validated
element - the form element that contains the val to validate elements by comparing them with other elements)
deferExceptions - do we want to deferValidation exceptions and attempt to validate all elements so that we can process all the exceptions at once
Throws:
ValidationException - if the element is not valid

validateFormElement

public void validateFormElement(Object val,
                                FormElement element,
                                FormMap map,
                                boolean deferExceptions)
                         throws ValidationException
Validate a FormElement. This is the method developers should override to provide specific validation based on the value and (potentially) information contained in the FormElement and FormMap structures. To indicate an element is invalid, through a ValidationException, which will interrupt the validation process immediately. If you want to indicate an error, but would still like validation to continue (so that you can identify multiple errors in one validation pass) throw a DeferredValidationException instead.

Parameters:
val - the actual value to be validated
element - the form element that contains the val
map - the map to which the element belongs (sometimes necessary to validate elements by comparing them with other elements)
deferExceptions - do we want to deferValidation exceptions and attempt to validate all elements so that we can process all the exceptions at once
Throws:
ValidationException - if the element is not valid


Copyright © 2006 BarracudaMVC.org All Rights Reserved.