From 5b0886f10b21fb9ab24cecd9e8d7fe687c9bfe97 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Wed, 13 Nov 2013 08:45:12 -0500 Subject: [PATCH] Allow "on" and "attributes" to take either array or string for validators. --- framework/yii/validators/Validator.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/framework/yii/validators/Validator.php b/framework/yii/validators/Validator.php index 7ea166d..36a1af4 100644 --- a/framework/yii/validators/Validator.php +++ b/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 = []; /** @@ -88,7 +89,8 @@ abstract class Validator extends Component */ 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 = []; /** @@ -131,7 +133,7 @@ abstract class Validator extends Component * @param array $params initial values to be applied to the validator properties * @return Validator the validator */ - public static function createValidator($type, $object, array $attributes, $params = []) + public static function createValidator($type, $object, $attributes, $params = []) { $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. * @param \yii\base\Model $object the data object being validated * @param array|null $attributes the list of attributes to be validated.