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.
271 lines
9.7 KiB
271 lines
9.7 KiB
14 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
14 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
14 years ago
|
namespace yii\validators;
|
||
|
|
||
12 years ago
|
use Yii;
|
||
12 years ago
|
use yii\base\Component;
|
||
12 years ago
|
use yii\base\NotSupportedException;
|
||
12 years ago
|
|
||
14 years ago
|
/**
|
||
14 years ago
|
* Validator is the base class for all validators.
|
||
14 years ago
|
*
|
||
12 years ago
|
* Child classes should override the [[validateAttribute()]] method to provide the actual
|
||
|
* logic of performing data validation. Child classes may also override [[clientValidateAttribute()]]
|
||
14 years ago
|
* to provide client-side validation support.
|
||
14 years ago
|
*
|
||
12 years ago
|
* Validator declares a set of [[builtInValidators|built-in validators] which can
|
||
14 years ago
|
* be referenced using short names. They are listed as follows:
|
||
|
*
|
||
14 years ago
|
* - `boolean`: [[BooleanValidator]]
|
||
|
* - `captcha`: [[CaptchaValidator]]
|
||
12 years ago
|
* - `compare`: [[CompareValidator]]
|
||
|
* - `date`: [[DateValidator]]
|
||
14 years ago
|
* - `default`: [[DefaultValueValidator]]
|
||
12 years ago
|
* - `double`: [[NumberValidator]]
|
||
|
* - `email`: [[EmailValidator]]
|
||
14 years ago
|
* - `exist`: [[ExistValidator]]
|
||
12 years ago
|
* - `file`: [[FileValidator]]
|
||
|
* - `filter`: [[FilterValidator]]
|
||
|
* - `in`: [[RangeValidator]]
|
||
|
* - `integer`: [[NumberValidator]]
|
||
|
* - `match`: [[RegularExpressionValidator]]
|
||
|
* - `required`: [[RequiredValidator]]
|
||
|
* - `string`: [[StringValidator]]
|
||
|
* - `unique`: [[UniqueValidator]]
|
||
|
* - `url`: [[UrlValidator]]
|
||
14 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
14 years ago
|
* @since 2.0
|
||
14 years ago
|
*/
|
||
12 years ago
|
abstract class Validator extends Component
|
||
14 years ago
|
{
|
||
|
/**
|
||
14 years ago
|
* @var array list of built-in validators (name => class or configuration)
|
||
14 years ago
|
*/
|
||
|
public static $builtInValidators = array(
|
||
12 years ago
|
'boolean' => 'yii\validators\BooleanValidator',
|
||
|
'captcha' => 'yii\validators\CaptchaValidator',
|
||
|
'compare' => 'yii\validators\CompareValidator',
|
||
|
'date' => 'yii\validators\DateValidator',
|
||
|
'default' => 'yii\validators\DefaultValueValidator',
|
||
|
'double' => 'yii\validators\NumberValidator',
|
||
|
'email' => 'yii\validators\EmailValidator',
|
||
|
'exist' => 'yii\validators\ExistValidator',
|
||
|
'file' => 'yii\validators\FileValidator',
|
||
|
'filter' => 'yii\validators\FilterValidator',
|
||
|
'in' => 'yii\validators\RangeValidator',
|
||
12 years ago
|
'integer' => array(
|
||
|
'class' => 'yii\validators\NumberValidator',
|
||
|
'integerOnly' => true,
|
||
|
),
|
||
12 years ago
|
'match' => 'yii\validators\RegularExpressionValidator',
|
||
12 years ago
|
'number' => 'yii\validators\NumberValidator',
|
||
12 years ago
|
'required' => 'yii\validators\RequiredValidator',
|
||
|
'string' => 'yii\validators\StringValidator',
|
||
|
'unique' => 'yii\validators\UniqueValidator',
|
||
|
'url' => 'yii\validators\UrlValidator',
|
||
14 years ago
|
);
|
||
|
|
||
|
/**
|
||
|
* @var array list of attributes to be validated.
|
||
|
*/
|
||
|
public $attributes;
|
||
|
/**
|
||
12 years ago
|
* @var string the user-defined error message. It may contain the following placeholders which
|
||
|
* will be replaced accordingly by the validator:
|
||
|
*
|
||
|
* - `{attribute}`: the label of the attribute being validated
|
||
|
* - `{value}`: the value of the attribute being validated
|
||
14 years ago
|
*/
|
||
|
public $message;
|
||
|
/**
|
||
12 years ago
|
* @var array list of scenarios that the validator can be applied to.
|
||
14 years ago
|
*/
|
||
12 years ago
|
public $on = array();
|
||
14 years ago
|
/**
|
||
12 years ago
|
* @var array list of scenarios that the validator should not be applied to.
|
||
|
*/
|
||
12 years ago
|
public $except = array();
|
||
12 years ago
|
/**
|
||
14 years ago
|
* @var boolean whether this validation rule should be skipped if the attribute being validated
|
||
12 years ago
|
* already has some validation error according to some previous rules. Defaults to true.
|
||
14 years ago
|
*/
|
||
13 years ago
|
public $skipOnError = true;
|
||
14 years ago
|
/**
|
||
12 years ago
|
* @var boolean whether this validation rule should be skipped if the attribute value
|
||
|
* is null or an empty string.
|
||
|
*/
|
||
|
public $skipOnEmpty = true;
|
||
12 years ago
|
/**
|
||
|
* @var boolean whether to enable client-side validation for this validator.
|
||
|
* The actual client-side validation is done via the JavaScript code returned
|
||
|
* by [[clientValidateAttribute()]]. If that method returns null, even if this property
|
||
|
* is true, no client-side validation will be done by this validator.
|
||
|
*/
|
||
|
public $enableClientValidation = true;
|
||
12 years ago
|
|
||
|
/**
|
||
14 years ago
|
* Validates a single attribute.
|
||
14 years ago
|
* Child classes must implement this method to provide the actual validation logic.
|
||
12 years ago
|
* @param \yii\base\Model $object the data object to be validated
|
||
14 years ago
|
* @param string $attribute the name of the attribute to be validated.
|
||
|
*/
|
||
14 years ago
|
abstract public function validateAttribute($object, $attribute);
|
||
14 years ago
|
|
||
|
/**
|
||
|
* Creates a validator object.
|
||
14 years ago
|
* @param string $type the validator type. This can be a method name,
|
||
12 years ago
|
* a built-in validator name, a class name, or a path alias of the validator class.
|
||
|
* @param \yii\base\Model $object the data object to be validated.
|
||
|
* @param array|string $attributes list of attributes to be validated. This can be either an array of
|
||
14 years ago
|
* the attribute names or a string of comma-separated attribute names.
|
||
|
* @param array $params initial values to be applied to the validator properties
|
||
14 years ago
|
* @return Validator the validator
|
||
14 years ago
|
*/
|
||
14 years ago
|
public static function createValidator($type, $object, $attributes, $params = array())
|
||
14 years ago
|
{
|
||
14 years ago
|
if (!is_array($attributes)) {
|
||
14 years ago
|
$attributes = preg_split('/[\s,]+/', $attributes, -1, PREG_SPLIT_NO_EMPTY);
|
||
14 years ago
|
}
|
||
12 years ago
|
$params['attributes'] = $attributes;
|
||
14 years ago
|
|
||
12 years ago
|
if (isset($params['on']) && !is_array($params['on'])) {
|
||
|
$params['on'] = preg_split('/[\s,]+/', $params['on'], -1, PREG_SPLIT_NO_EMPTY);
|
||
14 years ago
|
}
|
||
|
|
||
12 years ago
|
if (isset($params['except']) && !is_array($params['except'])) {
|
||
|
$params['except'] = preg_split('/[\s,]+/', $params['except'], -1, PREG_SPLIT_NO_EMPTY);
|
||
12 years ago
|
}
|
||
|
|
||
13 years ago
|
if (method_exists($object, $type)) {
|
||
|
// method-based validator
|
||
12 years ago
|
$params['class'] = __NAMESPACE__ . '\InlineValidator';
|
||
|
$params['method'] = $type;
|
||
13 years ago
|
} else {
|
||
12 years ago
|
if (isset(self::$builtInValidators[$type])) {
|
||
14 years ago
|
$type = self::$builtInValidators[$type];
|
||
|
}
|
||
12 years ago
|
if (is_array($type)) {
|
||
|
foreach ($type as $name => $value) {
|
||
|
$params[$name] = $value;
|
||
|
}
|
||
|
} else {
|
||
|
$params['class'] = $type;
|
||
|
}
|
||
14 years ago
|
}
|
||
|
|
||
12 years ago
|
return Yii::createObject($params);
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Validates the specified object.
|
||
14 years ago
|
* @param \yii\base\Model $object the data object being validated
|
||
12 years ago
|
* @param array|null $attributes the list of attributes to be validated.
|
||
|
* Note that if an attribute is not associated with the validator,
|
||
|
* it will be ignored.
|
||
|
* If this parameter is null, every attribute listed in [[attributes]] will be validated.
|
||
14 years ago
|
*/
|
||
|
public function validate($object, $attributes = null)
|
||
|
{
|
||
14 years ago
|
if (is_array($attributes)) {
|
||
14 years ago
|
$attributes = array_intersect($this->attributes, $attributes);
|
||
13 years ago
|
} else {
|
||
14 years ago
|
$attributes = $this->attributes;
|
||
14 years ago
|
}
|
||
|
foreach ($attributes as $attribute) {
|
||
12 years ago
|
$skip = $this->skipOnError && $object->hasErrors($attribute)
|
||
|
|| $this->skipOnEmpty && $this->isEmpty($object->$attribute);
|
||
|
if (!$skip) {
|
||
14 years ago
|
$this->validateAttribute($object, $attribute);
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Validates a value.
|
||
|
* A validator class can implement this method to support data validation out of the context of a data model.
|
||
|
* @param mixed $value the data value to be validated.
|
||
|
* @throws NotSupportedException if data validation without a model is not supported
|
||
|
*/
|
||
12 years ago
|
public function validateValue($value)
|
||
|
{
|
||
12 years ago
|
throw new NotSupportedException(get_class($this) . ' does not support validateValue().');
|
||
12 years ago
|
}
|
||
|
|
||
14 years ago
|
/**
|
||
|
* Returns the JavaScript needed for performing client-side validation.
|
||
14 years ago
|
*
|
||
|
* You may override this method to return the JavaScript validation code if
|
||
|
* the validator can support client-side validation.
|
||
|
*
|
||
|
* The following JavaScript variables are predefined and can be used in the validation code:
|
||
|
*
|
||
|
* - `attribute`: the name of the attribute being validated.
|
||
|
* - `value`: the value being validated.
|
||
|
* - `messages`: an array used to hold the validation error messages for the attribute.
|
||
|
*
|
||
|
* @param \yii\base\Model $object the data object being validated
|
||
14 years ago
|
* @param string $attribute the name of the attribute to be validated.
|
||
12 years ago
|
* @param \yii\base\View $view the view object that is going to be used to render views or view files
|
||
|
* containing a model form with this validator applied.
|
||
14 years ago
|
* @return string the client-side validation script. Null if the validator does not support
|
||
|
* client-side validation.
|
||
|
* @see \yii\web\ActiveForm::enableClientValidation
|
||
14 years ago
|
*/
|
||
12 years ago
|
public function clientValidateAttribute($object, $attribute, $view)
|
||
14 years ago
|
{
|
||
12 years ago
|
return null;
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Returns a value indicating whether the validator is active for the given scenario and attribute.
|
||
|
*
|
||
|
* A validator is active if
|
||
14 years ago
|
*
|
||
12 years ago
|
* - the validator's `on` property is empty, or
|
||
14 years ago
|
* - the validator's `on` property contains the specified scenario
|
||
|
*
|
||
14 years ago
|
* @param string $scenario scenario name
|
||
|
* @return boolean whether the validator applies to the specified scenario.
|
||
|
*/
|
||
12 years ago
|
public function isActive($scenario)
|
||
14 years ago
|
{
|
||
12 years ago
|
return !in_array($scenario, $this->except, true) && (empty($this->on) || in_array($scenario, $this->on, true));
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
14 years ago
|
* Adds an error about the specified attribute to the model object.
|
||
14 years ago
|
* This is a helper method that performs message selection and internationalization.
|
||
14 years ago
|
* @param \yii\base\Model $object the data object being validated
|
||
14 years ago
|
* @param string $attribute the attribute being validated
|
||
|
* @param string $message the error message
|
||
|
* @param array $params values for the placeholders in the error message
|
||
|
*/
|
||
14 years ago
|
public function addError($object, $attribute, $message, $params = array())
|
||
14 years ago
|
{
|
||
12 years ago
|
$value = $object->$attribute;
|
||
14 years ago
|
$params['{attribute}'] = $object->getAttributeLabel($attribute);
|
||
12 years ago
|
$params['{value}'] = is_array($value) ? 'array()' : $value;
|
||
14 years ago
|
$object->addError($attribute, strtr($message, $params));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Checks if the given value is empty.
|
||
|
* A value is considered empty if it is null, an empty array, or the trimmed result is an empty string.
|
||
|
* Note that this method is different from PHP empty(). It will return false when the value is 0.
|
||
|
* @param mixed $value the value to be checked
|
||
|
* @param boolean $trim whether to perform trimming before checking if the string is empty. Defaults to false.
|
||
|
* @return boolean whether the value is empty
|
||
|
*/
|
||
14 years ago
|
public function isEmpty($value, $trim = false)
|
||
14 years ago
|
{
|
||
14 years ago
|
return $value === null || $value === array() || $value === ''
|
||
12 years ago
|
|| $trim && is_scalar($value) && trim($value) === '';
|
||
14 years ago
|
}
|
||
|
}
|