Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/advanced/common/models/LoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function rules()
{
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
Expand Down
2 changes: 1 addition & 1 deletion apps/advanced/frontend/models/ContactForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function rules()
{
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
Expand Down
2 changes: 1 addition & 1 deletion apps/basic/models/ContactForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function rules()
{
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
Expand Down
2 changes: 1 addition & 1 deletion apps/basic/models/LoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function rules()
{
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
Expand Down
4 changes: 2 additions & 2 deletions extensions/gii/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ public function getDescription()
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'],
];
}

Expand Down
12 changes: 6 additions & 6 deletions extensions/gii/generators/controller/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ public function getDescription()
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.'],
]);
}

Expand Down
22 changes: 11 additions & 11 deletions extensions/gii/generators/crud/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ public function getDescription()
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'],
]);
}

Expand Down
16 changes: 8 additions & 8 deletions extensions/gii/generators/form/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ public function generate()
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.'],
]);
}

Expand Down
26 changes: 13 additions & 13 deletions extensions/gii/generators/model/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ public function getDescription()
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'],
]);
}

Expand Down Expand Up @@ -237,10 +237,10 @@ public function generateRules($table)

$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;
Expand Down
10 changes: 5 additions & 5 deletions extensions/gii/generators/module/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public function getDescription()
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'],
]);
}

Expand Down
12 changes: 6 additions & 6 deletions framework/yii/base/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,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.
Expand All @@ -128,7 +128,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
Expand Down Expand Up @@ -390,7 +390,7 @@ public function createValidators()
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.');
Expand Down
13 changes: 1 addition & 12 deletions framework/yii/validators/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,10 @@ abstract public function validateAttribute($object, $attribute);
* @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';
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/data/base/Singer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}
}
4 changes: 2 additions & 2 deletions tests/unit/data/validators/models/FakedValidationModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function createWithAttributes($attributes = [])
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'],
];
}
Expand Down Expand Up @@ -60,4 +60,4 @@ public function getAttributeLabel($attr)
{
return $attr;
}
}
}
8 changes: 4 additions & 4 deletions tests/unit/framework/validators/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testCreateValidator()
$val = TestValidator::createValidator(
'boolean',
$model,
'attr_test1, attr_test2',
['attr_test1', 'attr_test2'],
['on' => ['a', 'b']]
);
$this->assertInstanceOf(BooleanValidator::className(), $val);
Expand All @@ -48,13 +48,13 @@ public function testCreateValidator()
$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);
}
Expand Down