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
{
return [
// username and password are both required
['username, password', 'required'],
[['username', 'password'], 'required'],
// password is validated by validatePassword()
['password', 'validatePassword'],
// rememberMe must be a boolean value
......
......@@ -23,7 +23,7 @@ class ContactForm extends Model
{
return [
// 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', 'email'],
// verifyCode needs to be entered correctly
......
......@@ -23,7 +23,7 @@ class ContactForm extends Model
{
return [
// 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', 'email'],
// verifyCode needs to be entered correctly
......
......@@ -21,7 +21,7 @@ class LoginForm extends Model
{
return [
// username and password are both required
['username, password', 'required'],
[['username', 'password'], 'required'],
// password is validated by validatePassword()
['password', 'validatePassword'],
// rememberMe must be a boolean value
......
......@@ -178,8 +178,8 @@ abstract class Generator extends Model
public function rules()
{
return [
['template', 'required', 'message' => 'A code template must be selected.'],
['template', 'validateTemplate'],
[['template'], 'required', 'message' => 'A code template must be selected.'],
[['template'], 'validateTemplate'],
];
}
......
......@@ -69,12 +69,12 @@ class Generator extends \yii\gii\Generator
public function rules()
{
return array_merge(parent::rules(), [
['controller, actions, baseClass, ns', 'filter', 'filter' => 'trim'],
['controller, baseClass', 'required'],
['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.'],
['baseClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
['ns', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
[['controller', 'actions', 'baseClass', 'ns'], 'filter', 'filter' => 'trim'],
[['controller', 'baseClass'], 'required'],
[['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.'],
[['baseClass'], '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
public function rules()
{
return array_merge(parent::rules(), [
['moduleID, controllerClass, modelClass, searchModelClass, baseControllerClass', 'filter', 'filter' => 'trim'],
['modelClass, searchModelClass, controllerClass, baseControllerClass, indexWidgetType', 'required'],
['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', 'validateClass', 'params' => ['extends' => ActiveRecord::className()]],
['baseControllerClass', 'validateClass', 'params' => ['extends' => Controller::className()]],
['controllerClass', 'match', 'pattern' => '/Controller$/', 'message' => 'Controller class name must be suffixed with "Controller".'],
['controllerClass, searchModelClass', 'validateNewClass'],
['indexWidgetType', 'in', 'range' => ['grid', 'list']],
['modelClass', 'validateModelClass'],
['moduleID', 'validateModuleID'],
[['moduleID', 'controllerClass', 'modelClass', 'searchModelClass', 'baseControllerClass'], 'filter', 'filter' => 'trim'],
[['modelClass', 'searchModelClass', 'controllerClass', 'baseControllerClass', 'indexWidgetType'], 'required'],
[['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'], 'validateClass', 'params' => ['extends' => ActiveRecord::className()]],
[['baseControllerClass'], 'validateClass', 'params' => ['extends' => Controller::className()]],
[['controllerClass'], 'match', 'pattern' => '/Controller$/', 'message' => 'Controller class name must be suffixed with "Controller".'],
[['controllerClass', 'searchModelClass'], 'validateNewClass'],
[['indexWidgetType'], 'in', 'range' => ['grid', 'list']],
[['modelClass'], 'validateModelClass'],
[['moduleID'], 'validateModuleID'],
]);
}
......
......@@ -60,14 +60,14 @@ class Generator extends \yii\gii\Generator
public function rules()
{
return array_merge(parent::rules(), [
['modelClass, viewName, scenarioName, viewPath', 'filter', 'filter' => 'trim'],
['modelClass, viewName, viewPath', 'required'],
['modelClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
['modelClass', 'validateClass', 'params' => ['extends' => Model::className()]],
['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', 'validateViewPath'],
['scenarioName', 'match', 'pattern' => '/^[\w\\-]+$/', 'message' => 'Only word characters and dashes are allowed.'],
[['modelClass', 'viewName', 'scenarioName', 'viewPath'], 'filter', 'filter' => 'trim'],
[['modelClass', 'viewName', 'viewPath'], 'required'],
[['modelClass'], 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
[['modelClass'], 'validateClass', 'params' => ['extends' => Model::className()]],
[['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'], 'validateViewPath'],
[['scenarioName'], 'match', 'pattern' => '/^[\w\\-]+$/', 'message' => 'Only word characters and dashes are allowed.'],
]);
}
......
......@@ -53,17 +53,17 @@ class Generator extends \yii\gii\Generator
public function rules()
{
return array_merge(parent::rules(), [
['db, ns, tableName, modelClass, baseClass', 'filter', 'filter' => 'trim'],
['db, ns, tableName, baseClass', 'required'],
['db, modelClass', 'match', 'pattern' => '/^\w+$/', 'message' => 'Only word characters 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.'],
['db', 'validateDb'],
['ns', 'validateNamespace'],
['tableName', 'validateTableName'],
['modelClass', 'validateModelClass', 'skipOnEmpty' => false],
['baseClass', 'validateClass', 'params' => ['extends' => ActiveRecord::className()]],
['generateRelations, generateLabelsFromComments', 'boolean'],
[['db', 'ns', 'tableName', 'modelClass', 'baseClass'], 'filter', 'filter' => 'trim'],
[['db', 'ns', 'tableName', 'baseClass'], 'required'],
[['db', 'modelClass'], 'match', 'pattern' => '/^\w+$/', 'message' => 'Only word characters 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.'],
[['db'], 'validateDb'],
[['ns'], 'validateNamespace'],
[['tableName'], 'validateTableName'],
[['modelClass'], 'validateModelClass', 'skipOnEmpty' => false],
[['baseClass'], 'validateClass', 'params' => ['extends' => ActiveRecord::className()]],
[['generateRelations', 'generateLabelsFromComments'], 'boolean'],
]);
}
......@@ -237,10 +237,10 @@ class Generator extends \yii\gii\Generator
$rules = [];
foreach ($types as $type => $columns) {
$rules[] = "['" . implode(', ', $columns) . "', '$type']";
$rules[] = "[['" . implode(', ', $columns) . "'], '$type']";
}
foreach ($lengths as $length => $columns) {
$rules[] = "['" . implode(', ', $columns) . "', 'string', 'max' => $length]";
$rules[] = "[['" . implode(', ', $columns) . "'], 'string', 'max' => $length]";
}
return $rules;
......
......@@ -45,11 +45,11 @@ class Generator extends \yii\gii\Generator
public function rules()
{
return array_merge(parent::rules(), [
['moduleID, moduleClass', 'filter', 'filter' => 'trim'],
['moduleID, moduleClass', 'required'],
['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', 'validateModuleClass'],
[['moduleID', 'moduleClass'], 'filter', 'filter' => 'trim'],
[['moduleID', 'moduleClass'], 'required'],
[['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'], 'validateModuleClass'],
]);
}
......
......@@ -92,19 +92,19 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
*
* ~~~
* [
* 'attribute list',
* ['attribute1', 'attribute2'],
* 'validator type',
* 'on' => 'scenario name',
* 'on' => ['scenario1', 'scenario2'],
* ...other parameters...
* ]
* ~~~
*
* 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
* 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.
* - additional name-value pairs can be specified to initialize the corresponding validator properties.
* Please refer to individual validator class API for possible properties.
......@@ -129,7 +129,7 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
* ~~~
* [
* // built-in "required" validator
* ['username', 'required'],
* [['username', 'password'], 'required'],
* // built-in "string" validator customized with "min" and "max" properties
* ['username', 'string', 'min' => 3, 'max' => 12],
* // built-in "compare" validator that is used in "register" scenario only
......@@ -391,7 +391,7 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
if ($rule instanceof Validator) {
$validators->append($rule);
} 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);
} else {
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
* @param array $params initial values to be applied to the validator properties
* @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;
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)) {
// method-based validator
$params['class'] = __NAMESPACE__ . '\InlineValidator';
......
......@@ -14,9 +14,9 @@ class Singer extends Model
public function rules()
{
return [
['lastName', 'default', 'value' => 'Lennon'],
['lastName', 'required'],
['underscore_style', 'yii\captcha\CaptchaValidator'],
[['lastName'], 'default', 'value' => 'Lennon'],
[['lastName'], 'required'],
[['underscore_style'], 'yii\captcha\CaptchaValidator'],
];
}
}
......@@ -28,7 +28,7 @@ class FakedValidationModel extends Model
public function rules()
{
return [
['val_attr_a, val_attr_b', 'required', 'on' => 'reqTest'],
[['val_attr_a', 'val_attr_b'], 'required', 'on' => 'reqTest'],
['val_attr_c', 'integer'],
];
}
......@@ -60,4 +60,4 @@ class FakedValidationModel extends Model
{
return $attr;
}
}
\ No newline at end of file
}
......@@ -39,7 +39,7 @@ class ValidatorTest extends TestCase
$val = TestValidator::createValidator(
'boolean',
$model,
'attr_test1, attr_test2',
['attr_test1', 'attr_test2'],
['on' => ['a', 'b']]
);
$this->assertInstanceOf(BooleanValidator::className(), $val);
......@@ -48,13 +48,13 @@ class ValidatorTest extends TestCase
$val = TestValidator::createValidator(
'boolean',
$model,
'attr_test1, attr_test2',
['on' => 'a, b', 'except' => 'c,d,e']
['attr_test1', 'attr_test2'],
['on' => ['a', 'b'], 'except' => ['c', 'd', 'e']]
);
$this->assertInstanceOf(BooleanValidator::className(), $val);
$this->assertSame(['a', 'b'], $val->on);
$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->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