Commit 447bad5a by Qiang Xue

Merge pull request #1184 from slavcodev/only-arrays-in-rules

Using only arrays in rules instead comma-separated string
parents 75dc290a f57b3536
...@@ -21,7 +21,7 @@ class LoginForm extends Model ...@@ -21,7 +21,7 @@ class LoginForm extends Model
{ {
return [ return [
// username and password are both required // username and password are both required
['username, password', 'required'], [['username', 'password'], 'required'],
// password is validated by validatePassword() // password is validated by validatePassword()
['password', 'validatePassword'], ['password', 'validatePassword'],
// rememberMe must be a boolean value // rememberMe must be a boolean value
......
...@@ -23,7 +23,7 @@ class ContactForm extends Model ...@@ -23,7 +23,7 @@ class ContactForm extends Model
{ {
return [ return [
// name, email, subject and body are required // name, email, subject and body are required
['name, email, subject, body', 'required'], [['name', 'email', 'subject', 'body'], 'required'],
// email has to be a valid email address // email has to be a valid email address
['email', 'email'], ['email', 'email'],
// verifyCode needs to be entered correctly // verifyCode needs to be entered correctly
......
...@@ -23,7 +23,7 @@ class ContactForm extends Model ...@@ -23,7 +23,7 @@ class ContactForm extends Model
{ {
return [ return [
// name, email, subject and body are required // name, email, subject and body are required
['name, email, subject, body', 'required'], [['name', 'email', 'subject', 'body'], 'required'],
// email has to be a valid email address // email has to be a valid email address
['email', 'email'], ['email', 'email'],
// verifyCode needs to be entered correctly // verifyCode needs to be entered correctly
......
...@@ -21,7 +21,7 @@ class LoginForm extends Model ...@@ -21,7 +21,7 @@ class LoginForm extends Model
{ {
return [ return [
// username and password are both required // username and password are both required
['username, password', 'required'], [['username', 'password'], 'required'],
// password is validated by validatePassword() // password is validated by validatePassword()
['password', 'validatePassword'], ['password', 'validatePassword'],
// rememberMe must be a boolean value // rememberMe must be a boolean value
......
...@@ -178,8 +178,8 @@ abstract class Generator extends Model ...@@ -178,8 +178,8 @@ abstract class Generator extends Model
public function rules() public function rules()
{ {
return [ return [
['template', 'required', 'message' => 'A code template must be selected.'], [['template'], 'required', 'message' => 'A code template must be selected.'],
['template', 'validateTemplate'], [['template'], 'validateTemplate'],
]; ];
} }
......
...@@ -69,12 +69,12 @@ class Generator extends \yii\gii\Generator ...@@ -69,12 +69,12 @@ class Generator extends \yii\gii\Generator
public function rules() public function rules()
{ {
return array_merge(parent::rules(), [ return array_merge(parent::rules(), [
['controller, actions, baseClass, ns', 'filter', 'filter' => 'trim'], [['controller', 'actions', 'baseClass', 'ns'], 'filter', 'filter' => 'trim'],
['controller, baseClass', 'required'], [['controller', 'baseClass'], 'required'],
['controller', 'match', 'pattern' => '/^[a-z\\-\\/]*$/', 'message' => 'Only a-z, dashes (-) and slashes (/) are allowed.'], [['controller'], 'match', 'pattern' => '/^[a-z\\-\\/]*$/', 'message' => 'Only a-z, dashes (-) and slashes (/) are allowed.'],
['actions', 'match', 'pattern' => '/^[a-z\\-,\\s]*$/', 'message' => 'Only a-z, dashes (-), spaces and commas are allowed.'], [['actions'], 'match', 'pattern' => '/^[a-z\\-,\\s]*$/', 'message' => 'Only a-z, dashes (-), spaces and commas are allowed.'],
['baseClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'], [['baseClass'], 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
['ns', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'], [['ns'], 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
]); ]);
} }
......
...@@ -42,17 +42,17 @@ class Generator extends \yii\gii\Generator ...@@ -42,17 +42,17 @@ class Generator extends \yii\gii\Generator
public function rules() public function rules()
{ {
return array_merge(parent::rules(), [ return array_merge(parent::rules(), [
['moduleID, controllerClass, modelClass, searchModelClass, baseControllerClass', 'filter', 'filter' => 'trim'], [['moduleID', 'controllerClass', 'modelClass', 'searchModelClass', 'baseControllerClass'], 'filter', 'filter' => 'trim'],
['modelClass, searchModelClass, controllerClass, baseControllerClass, indexWidgetType', 'required'], [['modelClass', 'searchModelClass', 'controllerClass', 'baseControllerClass', 'indexWidgetType'], 'required'],
['searchModelClass', 'compare', 'compareAttribute' => 'modelClass', 'operator' => '!==', 'message' => 'Search Model Class must not be equal to Model Class.'], [['searchModelClass'], 'compare', 'compareAttribute' => 'modelClass', 'operator' => '!==', 'message' => 'Search Model Class must not be equal to Model Class.'],
['modelClass, controllerClass, baseControllerClass, searchModelClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'], [['modelClass', 'controllerClass', 'baseControllerClass', 'searchModelClass'], 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
['modelClass', 'validateClass', 'params' => ['extends' => ActiveRecord::className()]], [['modelClass'], 'validateClass', 'params' => ['extends' => ActiveRecord::className()]],
['baseControllerClass', 'validateClass', 'params' => ['extends' => Controller::className()]], [['baseControllerClass'], 'validateClass', 'params' => ['extends' => Controller::className()]],
['controllerClass', 'match', 'pattern' => '/Controller$/', 'message' => 'Controller class name must be suffixed with "Controller".'], [['controllerClass'], 'match', 'pattern' => '/Controller$/', 'message' => 'Controller class name must be suffixed with "Controller".'],
['controllerClass, searchModelClass', 'validateNewClass'], [['controllerClass', 'searchModelClass'], 'validateNewClass'],
['indexWidgetType', 'in', 'range' => ['grid', 'list']], [['indexWidgetType'], 'in', 'range' => ['grid', 'list']],
['modelClass', 'validateModelClass'], [['modelClass'], 'validateModelClass'],
['moduleID', 'validateModuleID'], [['moduleID'], 'validateModuleID'],
]); ]);
} }
......
...@@ -60,14 +60,14 @@ class Generator extends \yii\gii\Generator ...@@ -60,14 +60,14 @@ class Generator extends \yii\gii\Generator
public function rules() public function rules()
{ {
return array_merge(parent::rules(), [ return array_merge(parent::rules(), [
['modelClass, viewName, scenarioName, viewPath', 'filter', 'filter' => 'trim'], [['modelClass', 'viewName', 'scenarioName', 'viewPath'], 'filter', 'filter' => 'trim'],
['modelClass, viewName, viewPath', 'required'], [['modelClass', 'viewName', 'viewPath'], 'required'],
['modelClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'], [['modelClass'], 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
['modelClass', 'validateClass', 'params' => ['extends' => Model::className()]], [['modelClass'], 'validateClass', 'params' => ['extends' => Model::className()]],
['viewName', 'match', 'pattern' => '/^\w+[\\-\\/\w]*$/', 'message' => 'Only word characters, dashes and slashes are allowed.'], [['viewName'], 'match', 'pattern' => '/^\w+[\\-\\/\w]*$/', 'message' => 'Only word characters, dashes and slashes are allowed.'],
['viewPath', 'match', 'pattern' => '/^@?\w+[\\-\\/\w]*$/', 'message' => 'Only word characters, dashes, slashes and @ are allowed.'], [['viewPath'], 'match', 'pattern' => '/^@?\w+[\\-\\/\w]*$/', 'message' => 'Only word characters, dashes, slashes and @ are allowed.'],
['viewPath', 'validateViewPath'], [['viewPath'], 'validateViewPath'],
['scenarioName', 'match', 'pattern' => '/^[\w\\-]+$/', 'message' => 'Only word characters and dashes are allowed.'], [['scenarioName'], 'match', 'pattern' => '/^[\w\\-]+$/', 'message' => 'Only word characters and dashes are allowed.'],
]); ]);
} }
......
...@@ -53,17 +53,17 @@ class Generator extends \yii\gii\Generator ...@@ -53,17 +53,17 @@ class Generator extends \yii\gii\Generator
public function rules() public function rules()
{ {
return array_merge(parent::rules(), [ return array_merge(parent::rules(), [
['db, ns, tableName, modelClass, baseClass', 'filter', 'filter' => 'trim'], [['db', 'ns', 'tableName', 'modelClass', 'baseClass'], 'filter', 'filter' => 'trim'],
['db, ns, tableName, baseClass', 'required'], [['db', 'ns', 'tableName', 'baseClass'], 'required'],
['db, modelClass', 'match', 'pattern' => '/^\w+$/', 'message' => 'Only word characters are allowed.'], [['db', 'modelClass'], 'match', 'pattern' => '/^\w+$/', 'message' => 'Only word characters are allowed.'],
['ns, baseClass', 'match', 'pattern' => '/^[\w\\\\]+$/', 'message' => 'Only word characters and backslashes are allowed.'], [['ns', 'baseClass'], 'match', 'pattern' => '/^[\w\\\\]+$/', 'message' => 'Only word characters and backslashes are allowed.'],
['tableName', 'match', 'pattern' => '/^(\w+\.)?([\w\*]+)$/', 'message' => 'Only word characters, and optionally an asterisk and/or a dot are allowed.'], [['tableName'], 'match', 'pattern' => '/^(\w+\.)?([\w\*]+)$/', 'message' => 'Only word characters, and optionally an asterisk and/or a dot are allowed.'],
['db', 'validateDb'], [['db'], 'validateDb'],
['ns', 'validateNamespace'], [['ns'], 'validateNamespace'],
['tableName', 'validateTableName'], [['tableName'], 'validateTableName'],
['modelClass', 'validateModelClass', 'skipOnEmpty' => false], [['modelClass'], 'validateModelClass', 'skipOnEmpty' => false],
['baseClass', 'validateClass', 'params' => ['extends' => ActiveRecord::className()]], [['baseClass'], 'validateClass', 'params' => ['extends' => ActiveRecord::className()]],
['generateRelations, generateLabelsFromComments', 'boolean'], [['generateRelations', 'generateLabelsFromComments'], 'boolean'],
]); ]);
} }
...@@ -237,10 +237,10 @@ class Generator extends \yii\gii\Generator ...@@ -237,10 +237,10 @@ class Generator extends \yii\gii\Generator
$rules = []; $rules = [];
foreach ($types as $type => $columns) { foreach ($types as $type => $columns) {
$rules[] = "['" . implode(', ', $columns) . "', '$type']"; $rules[] = "[['" . implode(', ', $columns) . "'], '$type']";
} }
foreach ($lengths as $length => $columns) { foreach ($lengths as $length => $columns) {
$rules[] = "['" . implode(', ', $columns) . "', 'string', 'max' => $length]"; $rules[] = "[['" . implode(', ', $columns) . "'], 'string', 'max' => $length]";
} }
return $rules; return $rules;
......
...@@ -45,11 +45,11 @@ class Generator extends \yii\gii\Generator ...@@ -45,11 +45,11 @@ class Generator extends \yii\gii\Generator
public function rules() public function rules()
{ {
return array_merge(parent::rules(), [ return array_merge(parent::rules(), [
['moduleID, moduleClass', 'filter', 'filter' => 'trim'], [['moduleID', 'moduleClass'], 'filter', 'filter' => 'trim'],
['moduleID, moduleClass', 'required'], [['moduleID', 'moduleClass'], 'required'],
['moduleID', 'match', 'pattern' => '/^[\w\\-]+$/', 'message' => 'Only word characters and dashes are allowed.'], [['moduleID'], 'match', 'pattern' => '/^[\w\\-]+$/', 'message' => 'Only word characters and dashes are allowed.'],
['moduleClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'], [['moduleClass'], 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
['moduleClass', 'validateModuleClass'], [['moduleClass'], 'validateModuleClass'],
]); ]);
} }
......
...@@ -92,19 +92,19 @@ class Model extends Component implements IteratorAggregate, ArrayAccess ...@@ -92,19 +92,19 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
* *
* ~~~ * ~~~
* [ * [
* 'attribute list', * ['attribute1', 'attribute2'],
* 'validator type', * 'validator type',
* 'on' => 'scenario name', * 'on' => ['scenario1', 'scenario2'],
* ...other parameters... * ...other parameters...
* ] * ]
* ~~~ * ~~~
* *
* where * where
* *
* - attribute list: required, specifies the attributes (separated by commas) to be validated; * - attribute list: required, specifies the attributes array to be validated, for single attribute you can pass string;
* - validator type: required, specifies the validator to be used. It can be the name of a model * - validator type: required, specifies the validator to be used. It can be the name of a model
* class method, the name of a built-in validator, or a validator class name (or its path alias). * class method, the name of a built-in validator, or a validator class name (or its path alias).
* - on: optional, specifies the [[scenario|scenarios]] (separated by commas) when the validation * - on: optional, specifies the [[scenario|scenarios]] array when the validation
* rule can be applied. If this option is not set, the rule will apply to all scenarios. * rule can be applied. If this option is not set, the rule will apply to all scenarios.
* - additional name-value pairs can be specified to initialize the corresponding validator properties. * - additional name-value pairs can be specified to initialize the corresponding validator properties.
* Please refer to individual validator class API for possible properties. * Please refer to individual validator class API for possible properties.
...@@ -129,7 +129,7 @@ class Model extends Component implements IteratorAggregate, ArrayAccess ...@@ -129,7 +129,7 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
* ~~~ * ~~~
* [ * [
* // built-in "required" validator * // built-in "required" validator
* ['username', 'required'], * [['username', 'password'], 'required'],
* // built-in "string" validator customized with "min" and "max" properties * // built-in "string" validator customized with "min" and "max" properties
* ['username', 'string', 'min' => 3, 'max' => 12], * ['username', 'string', 'min' => 3, 'max' => 12],
* // built-in "compare" validator that is used in "register" scenario only * // built-in "compare" validator that is used in "register" scenario only
...@@ -391,7 +391,7 @@ class Model extends Component implements IteratorAggregate, ArrayAccess ...@@ -391,7 +391,7 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
if ($rule instanceof Validator) { if ($rule instanceof Validator) {
$validators->append($rule); $validators->append($rule);
} elseif (is_array($rule) && isset($rule[0], $rule[1])) { // attributes, validator type } elseif (is_array($rule) && isset($rule[0], $rule[1])) { // attributes, validator type
$validator = Validator::createValidator($rule[1], $this, $rule[0], array_slice($rule, 2)); $validator = Validator::createValidator($rule[1], $this, (array) $rule[0], array_slice($rule, 2));
$validators->append($validator); $validators->append($validator);
} else { } else {
throw new InvalidConfigException('Invalid validation rule: a rule must specify both attribute names and validator type.'); throw new InvalidConfigException('Invalid validation rule: a rule must specify both attribute names and validator type.');
......
...@@ -131,21 +131,10 @@ abstract class Validator extends Component ...@@ -131,21 +131,10 @@ 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, $attributes, $params = []) public static function createValidator($type, $object, array $attributes, $params = [])
{ {
if (!is_array($attributes)) {
$attributes = preg_split('/[\s,]+/', $attributes, -1, PREG_SPLIT_NO_EMPTY);
}
$params['attributes'] = $attributes; $params['attributes'] = $attributes;
if (isset($params['on']) && !is_array($params['on'])) {
$params['on'] = preg_split('/[\s,]+/', $params['on'], -1, PREG_SPLIT_NO_EMPTY);
}
if (isset($params['except']) && !is_array($params['except'])) {
$params['except'] = preg_split('/[\s,]+/', $params['except'], -1, PREG_SPLIT_NO_EMPTY);
}
if (method_exists($object, $type)) { if (method_exists($object, $type)) {
// method-based validator // method-based validator
$params['class'] = __NAMESPACE__ . '\InlineValidator'; $params['class'] = __NAMESPACE__ . '\InlineValidator';
......
...@@ -14,9 +14,9 @@ class Singer extends Model ...@@ -14,9 +14,9 @@ class Singer extends Model
public function rules() public function rules()
{ {
return [ return [
['lastName', 'default', 'value' => 'Lennon'], [['lastName'], 'default', 'value' => 'Lennon'],
['lastName', 'required'], [['lastName'], 'required'],
['underscore_style', 'yii\captcha\CaptchaValidator'], [['underscore_style'], 'yii\captcha\CaptchaValidator'],
]; ];
} }
} }
...@@ -28,7 +28,7 @@ class FakedValidationModel extends Model ...@@ -28,7 +28,7 @@ class FakedValidationModel extends Model
public function rules() public function rules()
{ {
return [ return [
['val_attr_a, val_attr_b', 'required', 'on' => 'reqTest'], [['val_attr_a', 'val_attr_b'], 'required', 'on' => 'reqTest'],
['val_attr_c', 'integer'], ['val_attr_c', 'integer'],
]; ];
} }
...@@ -60,4 +60,4 @@ class FakedValidationModel extends Model ...@@ -60,4 +60,4 @@ class FakedValidationModel extends Model
{ {
return $attr; return $attr;
} }
} }
\ No newline at end of file
...@@ -39,7 +39,7 @@ class ValidatorTest extends TestCase ...@@ -39,7 +39,7 @@ class ValidatorTest extends TestCase
$val = TestValidator::createValidator( $val = TestValidator::createValidator(
'boolean', 'boolean',
$model, $model,
'attr_test1, attr_test2', ['attr_test1', 'attr_test2'],
['on' => ['a', 'b']] ['on' => ['a', 'b']]
); );
$this->assertInstanceOf(BooleanValidator::className(), $val); $this->assertInstanceOf(BooleanValidator::className(), $val);
...@@ -48,13 +48,13 @@ class ValidatorTest extends TestCase ...@@ -48,13 +48,13 @@ class ValidatorTest extends TestCase
$val = TestValidator::createValidator( $val = TestValidator::createValidator(
'boolean', 'boolean',
$model, $model,
'attr_test1, attr_test2', ['attr_test1', 'attr_test2'],
['on' => 'a, b', 'except' => 'c,d,e'] ['on' => ['a', 'b'], 'except' => ['c', 'd', 'e']]
); );
$this->assertInstanceOf(BooleanValidator::className(), $val); $this->assertInstanceOf(BooleanValidator::className(), $val);
$this->assertSame(['a', 'b'], $val->on); $this->assertSame(['a', 'b'], $val->on);
$this->assertSame(['c', 'd', 'e'], $val->except); $this->assertSame(['c', 'd', 'e'], $val->except);
$val = TestValidator::createValidator('inlineVal', $model, 'val_attr_a'); $val = TestValidator::createValidator('inlineVal', $model, ['val_attr_a']);
$this->assertInstanceOf(InlineValidator::className(), $val); $this->assertInstanceOf(InlineValidator::className(), $val);
$this->assertSame('inlineVal', $val->method); $this->assertSame('inlineVal', $val->method);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment