~horux-dev/horux-webcli/thfo

« back to all changes in this revision

Viewing changes to yii/framework/validators/CFilterValidator.php

  • Committer: Thierry Forchelet
  • Date: 2011-02-25 13:30:15 UTC
  • Revision ID: thierry.forchelet@letux.ch-20110225133015-zxyj9w7sqv8ly971
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CFilterValidator class file.
 
4
 *
 
5
 * @author Qiang Xue <qiang.xue@gmail.com>
 
6
 * @link http://www.yiiframework.com/
 
7
 * @copyright Copyright &copy; 2008-2011 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 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
 * @version $Id: CFilterValidator.php 2799 2011-01-01 19:31:13Z qiang.xue $
 
27
 * @package system.validators
 
28
 * @since 1.0
 
29
 */
 
30
class CFilterValidator extends CValidator
 
31
{
 
32
        /**
 
33
         * @var callback the filter method
 
34
         */
 
35
        public $filter;
 
36
 
 
37
        /**
 
38
         * Validates the attribute of the object.
 
39
         * If there is any error, the error message is added to the object.
 
40
         * @param CModel $object the object being validated
 
41
         * @param string $attribute the attribute being validated
 
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
}