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

13 years ago
<?php
/**
13 years ago
* DefaultValueValidator 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
* 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
13 years ago
* specifying attribute default values in a dynamic way.
*
* @author Qiang Xue <qiang.xue@gmail.com>
13 years ago
* @since 2.0
13 years ago
*/
13 years ago
class DefaultValueValidator extends Validator
13 years ago
{
/**
* @var mixed the default value to be set to the specified attributes.
*/
public $value;
/**
13 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.
13 years ago
*/
public $setOnEmpty = true;
/**
* Validates the attribute of 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
{
13 years ago
if (!$this->setOnEmpty || $this->isEmpty($object->$attribute)) {
13 years ago
$object->$attribute = $this->value;
}
}
}