IntegerValidator.php 1.47 KB
Newer Older
w  
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
<?php
/**
 * IntegerValidator class file.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\validators;

/**
 * IntegerValidator validates that the attribute value is an integer.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class IntegerValidator extends NumberValidator
{
	/**
	 * @var string the regular expression for matching integers.
	 */
	public $pattern = '/^\s*[+-]?\d+\s*$/';

	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
	 * @param \yii\base\Model $object the object being validated
	 * @param string $attribute the attribute being validated
	 */
	public function validateAttribute($object, $attribute)
	{
		if ($this->message === null) {
			$this->message = Yii::t('yii', '{attribute} must be an integer.');
		}
		parent::validateAttribute($object, $attribute);
	}

	/**
	 * Returns the JavaScript needed for performing client-side validation.
	 * @param \yii\base\Model $object the data object being validated
	 * @param string $attribute the name of the attribute to be validated.
	 * @return string the client-side validation script.
	 */
	public function clientValidateAttribute($object, $attribute)
	{
		if ($this->message === null) {
			$this->message = Yii::t('yii', '{attribute} must be an integer.');
		}
		return parent::clientValidateAttribute($object, $attribute);
	}
}