DefaultValueValidator.php 1.1 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
8 9
namespace yii\validators;

w  
Qiang Xue committed
10
/**
w  
Qiang Xue committed
11 12 13
 * DefaultValueValidator sets the attribute to be the specified default value.
 *
 * DefaultValueValidator is not really a validator. It is provided mainly to allow
Qiang Xue committed
14
 * specifying attribute default values when they are empty.
w  
Qiang Xue committed
15 16
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
17
 * @since 2.0
w  
Qiang Xue committed
18
 */
w  
Qiang Xue committed
19
class DefaultValueValidator extends Validator
w  
Qiang Xue committed
20 21 22 23 24 25
{
	/**
	 * @var mixed the default value to be set to the specified attributes.
	 */
	public $value;
	/**
Qiang Xue committed
26 27
	 * @var boolean this property is overwritten to be false so that this validator will
	 * be applied when the value being validated is empty.
w  
Qiang Xue committed
28
	 */
Qiang Xue committed
29
	public $skipOnEmpty = false;
w  
Qiang Xue committed
30 31 32

	/**
	 * Validates the attribute of the object.
w  
Qiang Xue committed
33
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
34 35
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
36
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
37
	{
Qiang Xue committed
38
		if ($this->isEmpty($object->$attribute)) {
w  
Qiang Xue committed
39 40 41 42
			$object->$attribute = $this->value;
		}
	}
}