You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.2 KiB

13 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
13 years ago
namespace yii\validators;
use Yii;
12 years ago
use yii\helpers\Html;
13 years ago
/**
13 years ago
* RequiredValidator validates that the specified attribute does not have null or empty value.
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
13 years ago
* @since 2.0
13 years ago
*/
13 years ago
class RequiredValidator extends Validator
13 years ago
{
/**
* @var boolean whether to skip this validator if the value being validated is empty.
*/
public $skipOnEmpty = false;
/**
13 years ago
* @var mixed the desired value that the attribute must have.
13 years ago
* If this is null, the validator will validate that the specified attribute is not empty.
13 years ago
* 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.
13 years ago
* @see strict
13 years ago
*/
public $requiredValue;
/**
13 years ago
* @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.
13 years ago
*/
public $strict = false;
/**
* @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;
13 years ago
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = $this->requiredValue === null ? Yii::t('yii', '{attribute} cannot be blank.')
: Yii::t('yii', '{attribute} must be "{requiredValue}".');
}
}
/**
* @inheritdoc
13 years ago
*/
protected function validateValue($value)
{
if ($this->requiredValue === null) {
if ($this->strict && $value !== null || !$this->strict && !$this->isEmpty($value, true)) {
return null;
}
} elseif (!$this->strict && $value == $this->requiredValue || $this->strict && $value === $this->requiredValue) {
return null;
}
if ($this->requiredValue === null) {
return [$this->message, []];
} else {
return [$this->message, [
'requiredValue' => $this->requiredValue,
]];
}
}
/**
* @inheritdoc
13 years ago
*/
public function clientValidateAttribute($object, $attribute, $view)
13 years ago
{
$options = [];
13 years ago
if ($this->requiredValue !== null) {
$options['message'] = strtr($this->message, [
13 years ago
'{requiredValue}' => $this->requiredValue,
]);
12 years ago
$options['requiredValue'] = $this->requiredValue;
13 years ago
} else {
12 years ago
$options['message'] = $this->message;
}
if ($this->strict) {
$options['strict'] = 1;
13 years ago
}
12 years ago
$options['message'] = strtr($options['message'], [
12 years ago
'{attribute}' => $object->getAttributeLabel($attribute),
]);
12 years ago
ValidationAsset::register($view);
12 years ago
return 'yii.validation.required(value, messages, ' . json_encode($options) . ');';
13 years ago
}
}