A week ago, I said I was about to add support for validators on all widgets, and not only fields. I have a running prototype (including a nice JS validator!), but came to a conclusion that impacts the current design of validation.
Having validators on all widgets seems to be a must have since it allows to perform higher-level validation on e.g. repeaters, forms, etc. Currently, a validation rule is supposed to return a ValidationError which is attached to the widget invoking the validation rule.
This has several problems:
- not all widgets can provide visual feedback for a validation error, i.e. a form, a repeater or a class.
- form-level validation can lead to detect that a particular field is invalid, and the validation error should be associated to that particular field.
So I came to the conclusion that the new-style validator should set a validation error on a widget that supports it which isn't necessarily the widget being validated, and should return a boolean indicating whether it has set a validation error or not.
The interface is as follows:
public interfaceValidator {
boolean validate(Widget widget);
}Being able to set a validation error also requires a new interface for this purpose (as many classes have a setValidationError() method)
interface ValidationErrorAware {
ValidationError getValidationError();
void setValidationError(ValidationError error);
}
Note that this approach is fully compatible (semantically speaking) with the current approach, and the updated ValidationRule implementations will simply set a validation error on the widget they're given to validate.
WDYT?
Sylvain
-- Sylvain Wallez Anyware Technologies http://www.apache.org/~sylvain http://www.anyware-tech.com { XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects } Orixo, the opensource XML business alliance - http://www.orixo.com
