Browse Source

Allow "on" and "attributes" to take either array or string for validators.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
5b0886f10b
  1. 22
      framework/yii/validators/Validator.php

22
framework/yii/validators/Validator.php

@ -76,7 +76,8 @@ abstract class Validator extends Component
]; ];
/** /**
* @var array list of attributes to be validated. * @var array|string attributes to be validated by this validator. For multiple attributes,
* please specify them as an array; for single attribute, you may use either a string or an array.
*/ */
public $attributes = []; public $attributes = [];
/** /**
@ -88,7 +89,8 @@ abstract class Validator extends Component
*/ */
public $message; public $message;
/** /**
* @var array list of scenarios that the validator can be applied to. * @var array|string scenarios that the validator can be applied to. For multiple scenarios,
* please specify them as an array; for single scenario, you may use either a string or an array.
*/ */
public $on = []; public $on = [];
/** /**
@ -131,7 +133,7 @@ abstract class Validator extends Component
* @param array $params initial values to be applied to the validator properties * @param array $params initial values to be applied to the validator properties
* @return Validator the validator * @return Validator the validator
*/ */
public static function createValidator($type, $object, array $attributes, $params = []) public static function createValidator($type, $object, $attributes, $params = [])
{ {
$params['attributes'] = $attributes; $params['attributes'] = $attributes;
@ -156,6 +158,20 @@ abstract class Validator extends Component
} }
/** /**
* @inheritdoc
*/
public function init()
{
parent::init();
if (!is_array($this->attributes)) {
$this->attributes = (array)$this->attributes;
}
if (!is_array($this->on)) {
$this->on = (array)$this->on;
}
}
/**
* Validates the specified object. * Validates the specified object.
* @param \yii\base\Model $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param array|null $attributes the list of attributes to be validated. * @param array|null $attributes the list of attributes to be validated.

Loading…
Cancel
Save