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.

51 lines
1.5 KiB

14 years ago
<?php
/**
14 years ago
* DefaultValueValidator class file.
14 years ago
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
14 years ago
* @license http://www.yiiframework.com/license/
*/
14 years ago
namespace yii\validators;
14 years ago
/**
14 years ago
* DefaultValueValidator sets the attribute to be the specified default value.
*
* By default, when the attribute being validated is [[isEmpty|empty]], the validator
* will assign a default [[value]] to it. However, if [[setOnEmpty]] is false, the validator
* will always assign the default [[value]] to the attribute, no matter it is empty or not.
*
* DefaultValueValidator is not really a validator. It is provided mainly to allow
14 years ago
* specifying attribute default values in a dynamic way.
*
* @author Qiang Xue <qiang.xue@gmail.com>
14 years ago
* @since 2.0
14 years ago
*/
14 years ago
class DefaultValueValidator extends Validator
14 years ago
{
/**
* @var mixed the default value to be set to the specified attributes.
*/
public $value;
/**
14 years ago
* @var boolean whether to set the default [[value]] only when the attribute is [[isEmpty|empty]].
* Defaults to true. If false, the attribute will always be assigned with the default [[value]],
* no matter it is empty or not.
14 years ago
*/
public $setOnEmpty = true;
/**
* Validates the attribute of 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
{
14 years ago
if (!$this->setOnEmpty || $this->isEmpty($object->$attribute)) {
14 years ago
$object->$attribute = $this->value;
}
}
}