InlineValidator.php 3.2 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 29 30
	 * @var string|\Closure an anonymous function or the name of a model class method that will be
	 * called to perform the actual validation. Note that if you use anonymous function, you cannot
	 * use `$this` in it unless you are using PHP 5.4 or above.
w  
Qiang Xue committed
31 32 33 34 35 36
	 */
	public $method;
	/**
	 * @var array additional parameters that are passed to the validation method
	 */
	public $params;
Qiang Xue committed
37
	/**
Qiang Xue committed
38 39
	 * @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
40 41 42 43 44 45 46 47 48
	 *
	 * ~~~
	 * function foo($attribute)
	 * {
	 *     return "javascript";
	 * }
	 * ~~~
	 *
	 * where `$attribute` refers to the attribute name to be validated.
Qiang Xue committed
49 50
	 *
	 * Please refer to [[clientValidateAttribute()]] for details on how to return client validation code.
Qiang Xue committed
51 52
	 */
	public $clientValidate;
w  
Qiang Xue committed
53 54 55

	/**
	 * Validates the attribute of the object.
w  
Qiang Xue committed
56
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
57 58
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
59
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
60 61
	{
		$method = $this->method;
Qiang Xue committed
62 63 64 65
		if (is_string($method)) {
			$method = array($object, $method);
		}
		call_user_func($method, $attribute, $this->params);
w  
Qiang Xue committed
66
	}
Qiang Xue committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

	/**
	 * Returns the JavaScript needed for performing client-side validation.
	 *
	 * You may override this method to return the JavaScript validation code if
	 * the validator can support client-side validation.
	 *
	 * The following JavaScript variables are predefined and can be used in the validation code:
	 *
	 * - `attribute`: the name of the attribute being validated.
	 * - `value`: the value being validated.
	 * - `messages`: an array used to hold the validation error messages for the attribute.
	 *
	 * @param \yii\base\Model $object the data object being validated
	 * @param string $attribute the name of the attribute to be validated.
82 83
	 * @param \yii\base\View $view the view object that is going to be used to render views or view files
	 * containing a model form with this validator applied.
Qiang Xue committed
84 85 86 87 88
	 * @return string the client-side validation script. Null if the validator does not support
	 * client-side validation.
	 * @see enableClientValidation
	 * @see \yii\web\ActiveForm::enableClientValidation
	 */
89
	public function clientValidateAttribute($object, $attribute, $view)
Qiang Xue committed
90
	{
Alexander Makarov committed
91
		if ($this->clientValidate !== null) {
Qiang Xue committed
92
			$method = $this->clientValidate;
Qiang Xue committed
93 94 95 96
			if (is_string($method)) {
				$method = array($object, $method);
			}
			return call_user_func($method, $attribute);
Qiang Xue committed
97 98
		} else {
			return null;
Qiang Xue committed
99 100
		}
	}
Zander Baldwin committed
101
}