Chain Validation
Posted: Wed Mar 14, 2007 3:33 pm
As usual, I'm developing an application layer over the Zend Framework.. Now I've rewritten this a couple times and thought I would give my chain validation classes a clean slate. Now without getting into too much detail (I'd rather jump into code
), I'm looking for both design input and code ideas. I'll outline specifically my concerns through the code.
So my first concern with chain validation is the syntax to which people use.
A couple things to keep in mind,
* Validator can be chained together or run independently
* Errors are hardcoded within each rule, although errors should be overridable
* Attachments can optionally accept paramaters
A couple of concerns which come to mind.
*By having a single method attach() in the validator class, the class needs to use reflection to determine exactly what kind of object we are attaching. I don't really like this idea and usually avoid using reflection, but passing the object type to attach() seems even worse. Which do you prefer?
*If you'll notice, you are able to pass "flag" objects, such as Northern_Validator_Flag_Optional(), which would indicate to the validator to only perform the rule checking if certain requirements have been met. Such as only validate the field if the field has data in it. Now, I know the validator object needs to have some kind of optional setting, and I know I can create custom "flags", but again I'm kind of uncomfortable having a seperate object determining conditions.. Can anyone think of a better way to do this?
I'm only really concerned with the API, we'll worry about the internals in a bit. Mainly, I'm interested how others have tackled the API for (chain) validator classes
So my first concern with chain validation is the syntax to which people use.
A couple things to keep in mind,
* Validator can be chained together or run independently
* Errors are hardcoded within each rule, although errors should be overridable
* Attachments can optionally accept paramaters
Code: Select all
$validator = new Northern_Validator('test1', $this->getInvokeArg('get'));
$validator->attach('foo', new Northern_Validator_Rule_NotNull(), 'field cannot be empty');
$validator->attach('foo', new Northern_Validator_Filter_Trim());
$validator->attach('foo', new Northern_Validator_Flag_Optional());
$validator->dispatch();
if ($validator->isValid()) {
}
// or can be run as a chain
$validators[0] = new Northern_Validator('test1', $this->getInvokeArg('get'));
$validators[0]->attach('fieldname', new Northern_Validator_Rule_NotNull(), 'field cannot be empty');
$validators[0]->attach('fieldname', new Northern_Validator_Filter_Trim());
$validators[0]->attach('fieldname', new Northern_Validator_Flag_Optional());
$validators[1] = new Northern_Validator('test2', $this->getInvokeArg('post'));
$validators[1]->attach('fieldname1', new Northern_Validator_Rule_NotNull(), 'field cannot be empty');
$validators[1]->attach('fieldname2', new Northern_Validator_Filter_Trim());
$validators[1]->attach('fieldname2', new Northern_Validator_Flag_Optional());
$chain = new Northern_Validator_Chain($validators);
if ($chain->isValid()) {
}*By having a single method attach() in the validator class, the class needs to use reflection to determine exactly what kind of object we are attaching. I don't really like this idea and usually avoid using reflection, but passing the object type to attach() seems even worse. Which do you prefer?
*If you'll notice, you are able to pass "flag" objects, such as Northern_Validator_Flag_Optional(), which would indicate to the validator to only perform the rule checking if certain requirements have been met. Such as only validate the field if the field has data in it. Now, I know the validator object needs to have some kind of optional setting, and I know I can create custom "flags", but again I'm kind of uncomfortable having a seperate object determining conditions.. Can anyone think of a better way to do this?
I'm only really concerned with the API, we'll worry about the internals in a bit. Mainly, I'm interested how others have tackled the API for (chain) validator classes