DefaultValueValidator.php 979 Bytes
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

	/**
Qiang Xue committed
32
	 * @inheritdoc
w  
Qiang Xue committed
33
	 */
w  
Qiang Xue committed
34
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
35
	{
Qiang Xue committed
36
		if ($this->isEmpty($object->$attribute)) {
w  
Qiang Xue committed
37 38 39 40
			$object->$attribute = $this->value;
		}
	}
}