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.

126 lines
3.8 KiB

14 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
14 years ago
* @license http://www.yiiframework.com/license/
*/
14 years ago
namespace yii\validators;
use Yii;
14 years ago
/**
14 years ago
* RequiredValidator validates that the specified attribute does not have null or empty value.
14 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
14 years ago
* @since 2.0
14 years ago
*/
14 years ago
class RequiredValidator extends Validator
14 years ago
{
/**
* @var boolean whether to skip this validator if the value being validated is empty.
*/
public $skipOnEmpty = false;
/**
14 years ago
* @var mixed the desired value that the attribute must have.
14 years ago
* If this is null, the validator will validate that the specified attribute is not empty.
14 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.
14 years ago
* @see strict
14 years ago
*/
public $requiredValue;
/**
14 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.
14 years ago
*/
public $strict = false;
14 years ago
/**
* Initializes the validator.
*/
public function init()
{
parent::init();
if ($this->message === null) {
12 years ago
$this->message = $this->requiredValue === null ? Yii::t('yii|{attribute} cannot be blank.')
: Yii::t('yii|{attribute} must be "{requiredValue}".');
}
}
/**
14 years ago
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
14 years ago
* @param \yii\base\Model $object the object being validated
14 years ago
* @param string $attribute the attribute being validated
*/
14 years ago
public function validateAttribute($object, $attribute)
14 years ago
{
$value = $object->$attribute;
14 years ago
if ($this->requiredValue === null) {
if ($this->strict && $value === null || !$this->strict && $this->isEmpty($value, true)) {
$this->addError($object, $attribute, $this->message);
14 years ago
}
13 years ago
} else {
14 years ago
if (!$this->strict && $value != $this->requiredValue || $this->strict && $value !== $this->requiredValue) {
$this->addError($object, $attribute, $this->message, array(
'{requiredValue}' => $this->requiredValue,
));
14 years ago
}
14 years ago
}
}
/**
* 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;
}
/**
14 years ago
* Returns the JavaScript needed for performing client-side validation.
14 years ago
* @param \yii\base\Model $object the data object being validated
14 years ago
* @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script.
*/
public function clientValidateAttribute($object, $attribute)
{
14 years ago
if ($this->requiredValue !== null) {
$message = strtr($this->message, array(
14 years ago
'{attribute}' => $object->getAttributeLabel($attribute),
14 years ago
'{value}' => $object->$attribute,
14 years ago
'{requiredValue}' => $this->requiredValue,
14 years ago
));
return "
14 years ago
if (value != " . json_encode($this->requiredValue) . ") {
14 years ago
messages.push(" . json_encode($message) . ");
14 years ago
}
";
13 years ago
} else {
$message = strtr($this->message, array(
14 years ago
'{attribute}' => $object->getAttributeLabel($attribute),
14 years ago
'{value}' => $object->$attribute,
14 years ago
));
return "
14 years ago
if($.trim(value) == '') {
14 years ago
messages.push(" . json_encode($message) . ");
14 years ago
}
";
}
}
}