RequiredValidator.php 3.4 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * RequiredValidator 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
 * RequiredValidator validates that the specified attribute does not have null or empty value.
w  
Qiang Xue committed
14 15
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
16
 * @since 2.0
w  
Qiang Xue committed
17
 */
w  
Qiang Xue committed
18
class RequiredValidator extends Validator
w  
Qiang Xue committed
19 20 21
{
	/**
	 * @var mixed the desired value that the attribute must have.
w  
Qiang Xue committed
22
	 * If this is null, the validator will validate that the specified attribute is not empty.
w  
Qiang Xue committed
23 24 25
	 * 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
26
	 * @see strict
w  
Qiang Xue committed
27 28 29
	 */
	public $requiredValue;
	/**
w  
Qiang Xue committed
30 31 32 33 34 35
	 * @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
36 37
	 */
	public $strict = false;
w  
Qiang Xue committed
38

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

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