Reputation: 2020
Currently Im developing on a website with Zend framework.
I needed to develop a location field similar to the one found here: http://www.truelocal.com.au/
Which allow user to input PostCode Values (consist of 4 numbers) or Suburb Name
I'm stuck at the moment in the Zend form validation part
Basically I need to ensure the Postcode values or Suburb Name exist in the database.
I'm able to use Zend_Validate_Db_RecordExists
easily to check if the Postcode exist in my database.
$validator = new Zend_Validate_Db_RecordExists('postcodeTable', 'postcode');
But how can I also add another Suburb name Validation to check if Suburb Name exist in the database (Without conflicting with the Postcode validation check)?
Is there a clear and easy way to achieve this?
Do I need to write a custom validator? If so how can I do it?
Thanks so much in advance :)
Upvotes: 2
Views: 663
Reputation: 1927
I don't think you can alias validators, like you can decorators, but you could extend Zend_Validate_Db_RecordExists, for example:
class App_Validate_Db_SuburbExists
extends Zend_Validate_Db_RecordExists
{
}
Don't forget to add your library's validator prefixPath to the element e.g.
$element->addPrefixPath('App_Validate_Db', 'App/Validate/Db', 'validate');
Then you can add both validators with different options.
$element->addValidator(
new App_Validate_Db_SuburbExists('suburbTable', 'suburb'))
->addValidator(
new Zend_Validate_Db_RecordExists('postcodeTable', 'postcode'));
Upvotes: 1
Reputation: 2705
I think of custom validator which is just a proxy
something like this
$proxyValidator = new App_Validate_Proxy(); //implements Zend_Validate_Interface
$proxyValidator->addValidator($validateDb1);
$proxyValidator->addValidator($validateDb2);
at least one of subvalidators must return true to consider value as valid
$proxyValidator::isValid():
public function isValid($value)
{
foreach($this->_validators as $validator) {
$validator->isValid($value) ? return true : $this->_messages[] = $validator->getMessages();
}
return false;
}
Upvotes: 0