RequiredValidator.php 4.15 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
use Yii;
Qiang Xue committed
11
use yii\helpers\Html;
Qiang Xue committed
12

w  
Qiang Xue committed
13
/**
w  
Qiang Xue committed
14
 * RequiredValidator validates that the specified attribute does not have null or empty value.
w  
Qiang Xue committed
15 16
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
17
 * @since 2.0
w  
Qiang Xue committed
18
 */
w  
Qiang Xue committed
19
class RequiredValidator extends Validator
w  
Qiang Xue committed
20
{
Qiang Xue committed
21 22 23 24
	/**
	 * @var boolean whether to skip this validator if the value being validated is empty.
	 */
	public $skipOnEmpty = false;
w  
Qiang Xue committed
25 26
	/**
	 * @var mixed the desired value that the attribute must have.
w  
Qiang Xue committed
27
	 * If this is null, the validator will validate that the specified attribute is not empty.
w  
Qiang Xue committed
28 29 30
	 * 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
31
	 * @see strict
w  
Qiang Xue committed
32 33 34
	 */
	public $requiredValue;
	/**
w  
Qiang Xue committed
35 36 37 38 39 40
	 * @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
41 42
	 */
	public $strict = false;
43 44 45 46 47 48 49 50 51
	/**
	 * @var string the user-defined error message. It may contain the following placeholders which
	 * will be replaced accordingly by the validator:
	 *
	 * - `{attribute}`: the label of the attribute being validated
	 * - `{value}`: the value of the attribute being validated
	 * - `{requiredValue}`: the value of [[requiredValue]]
	 */
	public $message;
w  
Qiang Xue committed
52

Qiang Xue committed
53 54 55 56 57 58 59
	/**
	 * Initializes the validator.
	 */
	public function init()
	{
		parent::init();
		if ($this->message === null) {
60 61
			$this->message = $this->requiredValue === null ? Yii::t('yii', '{attribute} cannot be blank.')
				: Yii::t('yii', '{attribute} must be "{requiredValue}".');
Qiang Xue committed
62 63 64
		}
	}

w  
Qiang Xue committed
65 66 67
	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
68
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
69 70
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
71
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
72
	{
Qiang Xue committed
73 74
		if (!$this->validateValue($object->$attribute)) {
			if ($this->requiredValue === null) {
Qiang Xue committed
75
				$this->addError($object, $attribute, $this->message);
Qiang Xue committed
76
			} else {
Alexander Makarov committed
77
				$this->addError($object, $attribute, $this->message, [
78
					'requiredValue' => $this->requiredValue,
Alexander Makarov committed
79
				]);
w  
Qiang Xue committed
80
			}
w  
Qiang Xue committed
81 82 83
		}
	}

Qiang Xue committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	/**
	 * 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
101 102
	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
103
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
104
	 * @param string $attribute the name of the attribute to be validated.
Alexander Makarov committed
105
	 * @param \yii\web\View $view the view object that is going to be used to render views or view files
106
	 * containing a model form with this validator applied.
w  
Qiang Xue committed
107 108
	 * @return string the client-side validation script.
	 */
109
	public function clientValidateAttribute($object, $attribute, $view)
w  
Qiang Xue committed
110
	{
Alexander Makarov committed
111
		$options = [];
w  
Qiang Xue committed
112
		if ($this->requiredValue !== null) {
Alexander Makarov committed
113
			$options['message'] = strtr($this->message, [
w  
Qiang Xue committed
114
				'{requiredValue}' => $this->requiredValue,
Alexander Makarov committed
115
			]);
Qiang Xue committed
116
			$options['requiredValue'] = $this->requiredValue;
Qiang Xue committed
117
		} else {
Qiang Xue committed
118 119 120 121
			$options['message'] = $this->message;
		}
		if ($this->strict) {
			$options['strict'] = 1;
w  
Qiang Xue committed
122
		}
Qiang Xue committed
123

Alexander Makarov committed
124
		$options['message'] = Html::encode(strtr($options['message'], [
Qiang Xue committed
125
			'{attribute}' => $object->getAttributeLabel($attribute),
Alexander Makarov committed
126
		]));
Qiang Xue committed
127

128
		ValidationAsset::register($view);
Qiang Xue committed
129
		return 'yii.validation.required(value, messages, ' . json_encode($options) . ');';
w  
Qiang Xue committed
130 131
	}
}