InlineValidator.php 2.1 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
8 9
namespace yii\validators;

w  
Qiang Xue committed
10
/**
w  
Qiang Xue committed
11 12 13 14 15 16 17 18 19 20
 * InlineValidator represents a validator which is defined as a method in the object being validated.
 *
 * The validation method must have the following signature:
 *
 * ~~~
 * function foo($attribute, $params)
 * ~~~
 *
 * where `$attribute` refers to the name of the attribute being validated, while `$params`
 * is an array representing the additional parameters supplied in the validation rule.
w  
Qiang Xue committed
21 22
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
23
 * @since 2.0
w  
Qiang Xue committed
24
 */
w  
Qiang Xue committed
25
class InlineValidator extends Validator
w  
Qiang Xue committed
26 27
{
	/**
Qiang Xue committed
28
	 * @var string|\Closure an anonymous function or the name of a model class method that will be
Qiang Xue committed
29 30 31 32 33
	 * called to perform the actual validation. The signature of the method should be like the following:
	 *
	 * ~~~
	 * function foo($attribute, $params)
	 * ~~~
w  
Qiang Xue committed
34 35 36 37 38 39
	 */
	public $method;
	/**
	 * @var array additional parameters that are passed to the validation method
	 */
	public $params;
Qiang Xue committed
40
	/**
Qiang Xue committed
41 42
	 * @var string|\Closure an anonymous function or the name of a model class method that returns the client validation code.
	 * The signature of the method should be like the following:
Qiang Xue committed
43 44
	 *
	 * ~~~
Qiang Xue committed
45
	 * function foo($attribute, $params)
Qiang Xue committed
46 47 48 49 50 51
	 * {
	 *     return "javascript";
	 * }
	 * ~~~
	 *
	 * where `$attribute` refers to the attribute name to be validated.
Qiang Xue committed
52 53
	 *
	 * Please refer to [[clientValidateAttribute()]] for details on how to return client validation code.
Qiang Xue committed
54 55
	 */
	public $clientValidate;
w  
Qiang Xue committed
56 57

	/**
Qiang Xue committed
58
	 * @inheritdoc
w  
Qiang Xue committed
59
	 */
w  
Qiang Xue committed
60
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
61 62
	{
		$method = $this->method;
Qiang Xue committed
63
		if (is_string($method)) {
Alexander Makarov committed
64
			$method = [$object, $method];
Qiang Xue committed
65 66
		}
		call_user_func($method, $attribute, $this->params);
w  
Qiang Xue committed
67
	}
Qiang Xue committed
68 69

	/**
Qiang Xue committed
70
	 * @inheritdoc
Qiang Xue committed
71
	 */
72
	public function clientValidateAttribute($object, $attribute, $view)
Qiang Xue committed
73
	{
Alexander Makarov committed
74
		if ($this->clientValidate !== null) {
Qiang Xue committed
75
			$method = $this->clientValidate;
Qiang Xue committed
76
			if (is_string($method)) {
Alexander Makarov committed
77
				$method = [$object, $method];
Qiang Xue committed
78
			}
Qiang Xue committed
79
			return call_user_func($method, $attribute, $this->params);
Qiang Xue committed
80 81
		} else {
			return null;
Qiang Xue committed
82 83
		}
	}
Zander Baldwin committed
84
}