RequiredValidator.php 3.35 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
 * RequiredValidator validates that the specified attribute does not have null or empty value.
w  
Qiang Xue committed
12 13
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
14
 * @since 2.0
w  
Qiang Xue committed
15
 */
w  
Qiang Xue committed
16
class RequiredValidator extends Validator
w  
Qiang Xue committed
17 18 19
{
	/**
	 * @var mixed the desired value that the attribute must have.
w  
Qiang Xue committed
20
	 * If this is null, the validator will validate that the specified attribute is not empty.
w  
Qiang Xue committed
21 22 23
	 * If this is set as a value that is not null, the validator will validate that
	 * the attribute has a value that is the same as this property value.
	 * Defaults to null.
w  
Qiang Xue committed
24
	 * @see strict
w  
Qiang Xue committed
25 26 27
	 */
	public $requiredValue;
	/**
w  
Qiang Xue committed
28 29 30 31 32 33
	 * @var boolean whether the comparison between the attribute value and [[requiredValue]] is strict.
	 * When this is true, both the values and types must match.
	 * Defaults to false, meaning only the values need to match.
	 * Note that when [[requiredValue]] is null, if this property is true, the validator will check
	 * if the attribute value is null; If this property is false, the validator will call [[isEmpty]]
	 * to check if the attribute value is empty.
w  
Qiang Xue committed
34 35
	 */
	public $strict = false;
w  
Qiang Xue committed
36

w  
Qiang Xue committed
37 38 39
	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
40
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
41 42
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
43
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
44 45
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
46 47
		if ($this->requiredValue === null) {
			if ($this->strict && $value === null || !$this->strict && $this->isEmpty($value, true)) {
48
				$message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} cannot be blank.');
w  
Qiang Xue committed
49 50
				$this->addError($object, $attribute, $message);
			}
Qiang Xue committed
51
		} else {
w  
Qiang Xue committed
52
			if (!$this->strict && $value != $this->requiredValue || $this->strict && $value !== $this->requiredValue) {
53
				$message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} must be "{requiredValue}".');
Qiang Xue committed
54 55 56
				$this->addError($object, $attribute, $message, array(
					'{requiredValue}' => $this->requiredValue,
				));
w  
Qiang Xue committed
57
			}
w  
Qiang Xue committed
58 59 60 61 62
		}
	}

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
63
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
64 65 66 67 68 69
	 * @param string $attribute the name of the attribute to be validated.
	 * @return string the client-side validation script.
	 */
	public function clientValidateAttribute($object, $attribute)
	{
		$message = $this->message;
w  
Qiang Xue committed
70 71
		if ($this->requiredValue !== null) {
			if ($message === null) {
72
				$message = \Yii::t('yii|{attribute} must be "{requiredValue}".');
w  
Qiang Xue committed
73
			}
w  
Qiang Xue committed
74 75
			$message = strtr($message, array(
				'{attribute}' => $object->getAttributeLabel($attribute),
w  
Qiang Xue committed
76
				'{value}' => $object->$attribute,
w  
Qiang Xue committed
77
				'{requiredValue}' => $this->requiredValue,
w  
Qiang Xue committed
78 79
			));
			return "
w  
Qiang Xue committed
80
if (value != " . json_encode($this->requiredValue) . ") {
w  
Qiang Xue committed
81
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
82 83
}
";
Qiang Xue committed
84
		} else {
w  
Qiang Xue committed
85
			if ($message === null) {
86
				$message = \Yii::t('yii|{attribute} cannot be blank.');
w  
Qiang Xue committed
87
			}
w  
Qiang Xue committed
88 89
			$message = strtr($message, array(
				'{attribute}' => $object->getAttributeLabel($attribute),
w  
Qiang Xue committed
90
				'{value}' => $object->$attribute,
w  
Qiang Xue committed
91 92
			));
			return "
w  
Qiang Xue committed
93
if($.trim(value) == '') {
w  
Qiang Xue committed
94
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
95 96 97 98 99
}
";
		}
	}
}