1: <?php
2: /**
3: * CFilterValidator class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CFilterValidator transforms the data being validated based on a filter.
13: *
14: * CFilterValidator is actually not a validator but a data processor.
15: * It invokes the specified filter method to process the attribute value
16: * and save the processed value back to the attribute. The filter method
17: * must follow the following signature:
18: * <pre>
19: * function foo($value) {...return $newValue; }
20: * </pre>
21: * Many PHP 'built in' functions qualify this signature (e.g. trim).
22: *
23: * To specify the filter method, set {@link filter} property to be the function name.
24: *
25: * @author Qiang Xue <qiang.xue@gmail.com>
26: * @package system.validators
27: * @since 1.0
28: */
29: class CFilterValidator extends CValidator
30: {
31: /**
32: * @var callback the filter method
33: */
34: public $filter;
35:
36: /**
37: * Validates the attribute of the object.
38: * If there is any error, the error message is added to the object.
39: * @param CModel $object the object being validated
40: * @param string $attribute the attribute being validated
41: * @throws CException if given {@link filter} is not callable
42: */
43: protected function validateAttribute($object,$attribute)
44: {
45: if($this->filter===null || !is_callable($this->filter))
46: throw new CException(Yii::t('yii','The "filter" property must be specified with a valid callback.'));
47: $object->$attribute=call_user_func_array($this->filter,array($object->$attribute));
48: }
49: }
50: