Yii2 Bootstrap 3
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.

101 lines
3.4 KiB

13 years ago
<?php
/**
13 years ago
* RequiredValidator class file.
13 years ago
*
* @link http://www.yiiframework.com/
13 years ago
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
13 years ago
namespace yii\validators;
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 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;
13 years ago
/**
13 years ago
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
13 years ago
* @param \yii\base\Model $object the object being validated
13 years ago
* @param string $attribute the attribute being validated
*/
13 years ago
public function validateAttribute($object, $attribute)
13 years ago
{
$value = $object->$attribute;
13 years ago
if ($this->requiredValue === null) {
if ($this->strict && $value === null || !$this->strict && $this->isEmpty($value, true)) {
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} cannot be blank.');
13 years ago
$this->addError($object, $attribute, $message);
}
13 years ago
} else {
13 years ago
if (!$this->strict && $value != $this->requiredValue || $this->strict && $value !== $this->requiredValue) {
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be "{requiredValue}".',
array('{requiredValue}' => $this->requiredValue));
$this->addError($object, $attribute, $message);
}
13 years ago
}
}
/**
* Returns the JavaScript needed for performing client-side validation.
13 years ago
* @param \yii\base\Model $object the data object being validated
13 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)
{
$message = $this->message;
13 years ago
if ($this->requiredValue !== null) {
if ($message === null) {
$message = Yii::t('yii', '{attribute} must be "{requiredValue}".');
}
13 years ago
$message = strtr($message, array(
'{attribute}' => $object->getAttributeLabel($attribute),
13 years ago
'{value}' => $object->$attribute,
13 years ago
'{requiredValue}' => $this->requiredValue,
13 years ago
));
return "
13 years ago
if (value != " . json_encode($this->requiredValue) . ") {
13 years ago
messages.push(" . json_encode($message) . ");
13 years ago
}
";
13 years ago
} else {
13 years ago
if ($message === null) {
13 years ago
$message = Yii::t('yii', '{attribute} cannot be blank.');
13 years ago
}
13 years ago
$message = strtr($message, array(
'{attribute}' => $object->getAttributeLabel($attribute),
13 years ago
'{value}' => $object->$attribute,
13 years ago
));
return "
13 years ago
if($.trim(value) == '') {
13 years ago
messages.push(" . json_encode($message) . ");
13 years ago
}
";
}
}
}