RequiredValidator.php 3.83 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;

Qiang Xue committed
10 11
use Yii;

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
{
Qiang Xue committed
20 21 22 23
	/**
	 * @var boolean whether to skip this validator if the value being validated is empty.
	 */
	public $skipOnEmpty = false;
w  
Qiang Xue committed
24 25
	/**
	 * @var mixed the desired value that the attribute must have.
w  
Qiang Xue committed
26
	 * If this is null, the validator will validate that the specified attribute is not empty.
w  
Qiang Xue committed
27 28 29
	 * 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
30
	 * @see strict
w  
Qiang Xue committed
31 32 33
	 */
	public $requiredValue;
	/**
w  
Qiang Xue committed
34 35 36 37 38 39
	 * @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
40 41
	 */
	public $strict = false;
w  
Qiang Xue committed
42

Qiang Xue committed
43 44 45 46 47 48 49
	/**
	 * Initializes the validator.
	 */
	public function init()
	{
		parent::init();
		if ($this->message === null) {
Qiang Xue committed
50
			$this->message = $this->requiredValue === null ? Yii::t('yii|{attribute} cannot be blank.')
Qiang Xue committed
51
				: Yii::t('yii|{attribute} must be "{requiredValue}".');
Qiang Xue committed
52 53 54
		}
	}

w  
Qiang Xue committed
55 56 57
	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
58
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
59 60
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
61
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
62 63
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
64 65
		if ($this->requiredValue === null) {
			if ($this->strict && $value === null || !$this->strict && $this->isEmpty($value, true)) {
Qiang Xue committed
66
				$this->addError($object, $attribute, $this->message);
w  
Qiang Xue committed
67
			}
Qiang Xue committed
68
		} else {
w  
Qiang Xue committed
69
			if (!$this->strict && $value != $this->requiredValue || $this->strict && $value !== $this->requiredValue) {
Qiang Xue committed
70
				$this->addError($object, $attribute, $this->message, array(
Qiang Xue committed
71 72
					'{requiredValue}' => $this->requiredValue,
				));
w  
Qiang Xue committed
73
			}
w  
Qiang Xue committed
74 75 76
		}
	}

Qiang Xue committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	/**
	 * Validates the given value.
	 * @param mixed $value the value to be validated.
	 * @return boolean whether the value is valid.
	 */
	public function validateValue($value)
	{
		if ($this->requiredValue === null) {
			if ($this->strict && $value !== null || !$this->strict && !$this->isEmpty($value, true)) {
				return true;
			}
		} elseif (!$this->strict && $value == $this->requiredValue || $this->strict && $value === $this->requiredValue) {
			return true;
		}
		return false;
	}

w  
Qiang Xue committed
94 95
	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
96
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
97 98 99 100 101
	 * @param string $attribute the name of the attribute to be validated.
	 * @return string the client-side validation script.
	 */
	public function clientValidateAttribute($object, $attribute)
	{
w  
Qiang Xue committed
102
		if ($this->requiredValue !== null) {
Qiang Xue committed
103
			$message = strtr($this->message, array(
w  
Qiang Xue committed
104
				'{attribute}' => $object->getAttributeLabel($attribute),
w  
Qiang Xue committed
105
				'{value}' => $object->$attribute,
w  
Qiang Xue committed
106
				'{requiredValue}' => $this->requiredValue,
w  
Qiang Xue committed
107 108
			));
			return "
w  
Qiang Xue committed
109
if (value != " . json_encode($this->requiredValue) . ") {
w  
Qiang Xue committed
110
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
111 112
}
";
Qiang Xue committed
113
		} else {
Qiang Xue committed
114
			$message = strtr($this->message, array(
w  
Qiang Xue committed
115
				'{attribute}' => $object->getAttributeLabel($attribute),
w  
Qiang Xue committed
116
				'{value}' => $object->$attribute,
w  
Qiang Xue committed
117 118
			));
			return "
w  
Qiang Xue committed
119
if($.trim(value) == '') {
w  
Qiang Xue committed
120
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
121 122 123 124 125
}
";
		}
	}
}