InlineValidator.php 2.7 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * InlineValidator class file.
w  
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
w  
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
10 11
namespace yii\validators;

w  
Qiang Xue committed
12
/**
w  
Qiang Xue committed
13 14 15 16 17 18 19 20 21 22
 * 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
23 24
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
25
 * @since 2.0
w  
Qiang Xue committed
26
 */
w  
Qiang Xue committed
27
class InlineValidator extends Validator
w  
Qiang Xue committed
28 29
{
	/**
Alexander Makarov committed
30 31
	 * @var string the name of the validation method defined in the
	 * \yii\base\Model class
w  
Qiang Xue committed
32 33 34 35 36 37
	 */
	public $method;
	/**
	 * @var array additional parameters that are passed to the validation method
	 */
	public $params;
Qiang Xue committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51
	/**
	 * @var string the name of the method that returns the client validation code (see [[clientValidateAttribute()]]
	 * for details on how to return client validation code). The signature of the method should be like the following:
	 *
	 * ~~~
	 * function foo($attribute)
	 * {
	 *     return "javascript";
	 * }
	 * ~~~
	 *
	 * where `$attribute` refers to the attribute name to be validated.
	 */
	public $clientValidate;
w  
Qiang Xue committed
52 53 54

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

	/**
	 * 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.
	 * @return string the client-side validation script. Null if the validator does not support
	 * client-side validation.
	 * @see enableClientValidation
	 * @see \yii\web\ActiveForm::enableClientValidation
	 */
	public function clientValidateAttribute($object, $attribute)
	{
Alexander Makarov committed
85
		if ($this->clientValidate !== null) {
Qiang Xue committed
86 87
			$method = $this->clientValidate;
			return $object->$method($attribute);
Qiang Xue committed
88 89
		} else {
			return null;
Qiang Xue committed
90 91
		}
	}
Alexander Makarov committed
92
}