Пользоваться так:
$this->validatorSchema['clothes_size']=new sfValidatorNumberEvenOdd(array(
'min'=>'40',
'max'=>'70',
'even_odd'=>'even',
),
array(
'min'=>'Некорректное значение',
'max'=>'Некорректное значение',
'even'=>'Размер одежды должен быть четным',
)
);
/**
* sfValidatorBoolean validates a number is even or odd.
*
* @author Evgeny Babin <psylosss@gmail.com>
*/
class sfValidatorNumberEvenOdd extends sfValidatorNumber
{
/**
* Configures the current validator.
*
* Available options:
*
* * even_odd: even|odd
*
* @param array $options An array of options
* @param array $messages An array of error messages
*
* @see sfValidatorNumber
*/
protected function configure($options = array(), $messages = array())
{
parent::configure($options,$messages);
$this->addMessage('odd', 'Only odd values accepted.');
$this->addMessage('even', 'Only even values accepted.');
$this->addMessage('invalid_option_value', 'Option even_odd accepts only "even" or "odd", %option% given.');
$this->addRequiredOption('even_odd');
}
/**
* @see sfValidatorBase
*/
protected function doClean($value)
{
$clean=parent::doClean($value);
if (!in_array($this->getOption('even_odd'),array('even','odd')))
{
throw new sfValidatorError($this, 'invalid_option_value', array('option' => $this->getOption('even_odd')));
}
if ($this->getOption('even_odd')=='even' and ($value % 2 == 1))
{
throw new sfValidatorError($this, 'even', array('value' => $value));
}
if ($this->getOption('even_odd')=='odd' and ($value % 2 == 0))
{
throw new sfValidatorError($this, 'odd', array('value' => $value));
}
return $clean;
}
}
Комментариев нет:
Отправить комментарий