Creating a custom form validator using Zend_Validate
Zend_Validate_Interface allows to create any type of validator, though a Zend_Validate_Regex exists, I created my own URL validator using a regular expression just for the sake of this example.
You can see the custom validator in action in this post.
This is the code:
class My_Validate_Url extends Zend_Validate_Abstract
{
/**
* Validation failure message key for when the value is an empty string
*/
const STRING_EMPTY = 'stringEmpty';
const NOT_MATCH = 'urlNotMatch';
/**
* @var array
*/
protected $_messageTemplates = array(
self::NOT_MATCH => "'%value%' does not match an url",
self::STRING_EMPTY => "'%value% is an empty string"
);
public function isValid($value)
{
$valueString = (string) $value;
$this->_setValue($valueString);
if ('' === $valueString) {
$this->_error(self::STRING_EMPTY);
return false;
} elseif (!preg_match('#https?://([-\w\.])+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?#', $valueString)) {
$this->_error(self::NOT_MATCH);
return false;
}
return true;
}
}
Tags: form validation, php, zend framework
No Comments
Comments RSS
TrackBack Identifier URI
No comments. Be the first.
Leave a comment


