Hi Gavin and Ziffers
Just to keep you in the loop Ralf and I are halfway through the
Zend_Form component which tackles real-world implementations of
Zend_FilterChain and Zend_Validator. Rather than wait for
Christopher's proposal to be finalised, we've pushed these into
Zend_Form so we can start using them and see what issues we come up
against.
The two components above have been integrated as Zend_Form_Validator
and Zend_Form_Rule which act as chains for a set of
Zend_Form_Validator_xxx / Zend_Form_Rule_xxx respectively. Rather
than bind these to a single source, though, Zend_Form creates these
validators on a per-object basis to allow less looping through the
chains and more light-weight/targetted validators and filters. So far
they are working like a champ, thanks to Christopher's code, but
we're yet to determine how much they can be abstracted for more
generic Zend_Rule and Zend_Validate components.
Here's how they look so far:-
<?php
class Zend_Form_Filter
{
protected $_chain = array();
/**
* Add a rule object to the chain
*
* @param string $key
* @param object $rule
* @return null
*/
public function add(Zend_Form_Filter_Interface $filter)
{
array_push($this->_chain, $filter);
}
/**
* Run all rules in chain and get messages
*
* @param mixed $field
*/
public function filter(Zend_Form_Field $field)
{
foreach ($this->_chain as $filter) {
$field->setValue($filter->filter($field->getValue()));
}
}
}
<?php
class Zend_Form_Validator
{
protected $_chain = array();
protected $_error = array();
/**
* Add a rule object to the chain
*
* @param object $rule
* @return null
*/
public function add(Zend_Form_Rule $rule)
{
array_push($this->_chain, $rule);
}
/**
* Run all rules in chain and get messages
*
* @param object $field
* @return mixed
*/
public function validate(Zend_Form_Field $field)
{
foreach ($this->_chain as $rule) {
if (($result = $rule->validate($field->getValue())) !==
true) {
array_push($this->_error, $result);
}
}
return empty($this->_error);
}
/**
* get array of all error messages or those for a specific key
*
* @param mixed $key
* @return mixed
*/
public function getError()
{
return $this->_error;
}
}
// An example of a simple regex validator
class Zend_Form_Rule_Regex extends Zend_Form_Rule
{
protected $_args;
/*
* We're using identifiers rather than full strings so that we can
* match these up to 'rea'l messages in the view (for
translation, etc)
*/
protected $_message = 'non_regex';
public function __construct($args = null, $message = null)
{
if (!is_null($args)) {
$this->_args = $args;
}
if (!is_null($message)) {
$this->_message = $message;
}
}
/**
* Check result
*
* @param mixed $value
* @return string|boolean
*/
public function validate($value)
{
if ($value != '' && !preg_match($this->_args, $value)) {
return $this->_message;
}
return true;
}
}
// An example of a simple trim filter
class Zend_Form_Filter_Trim extends Zend_Form_Filter_Abstract
{
/**
* Trim whitespace
*
* @param mixed $value
* @return mixed
*/
public function filter($value)
{
if (is_array($value)) {
foreach ($value as $key => $val) {
$value[$key] = $this->filter($val);
}
} else {
$value = trim($value);
}
return $value;
}
}
Will keep everyone posted when the proposal as a whole has come along
a bit further and post code on the proposal Wiki.
Cheerio
--
Simon Mundy | Director | PEPTOLAB
""" " "" """""" "" "" """"""" " "" """"" " """"" " """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654
4124
http://www.peptolab.com