Browse Source

Refactor all models to new rules format

tags/2.0.0-beta
slavcodev 11 years ago
parent
commit
ecc557ef92
  1. 6
      apps/advanced/common/models/LoginForm.php
  2. 24
      apps/advanced/common/models/User.php
  3. 6
      apps/advanced/frontend/models/ContactForm.php
  4. 6
      apps/basic/models/ContactForm.php
  5. 6
      apps/basic/models/LoginForm.php
  6. 4
      extensions/gii/Generator.php
  7. 12
      extensions/gii/generators/controller/Generator.php
  8. 22
      extensions/gii/generators/crud/Generator.php
  9. 16
      extensions/gii/generators/form/Generator.php
  10. 22
      extensions/gii/generators/model/Generator.php
  11. 10
      extensions/gii/generators/module/Generator.php
  12. 6
      tests/unit/data/base/Singer.php
  13. 6
      tests/unit/data/validators/models/FakedValidationModel.php

6
apps/advanced/common/models/LoginForm.php

@ -21,11 +21,11 @@ class LoginForm extends Model
{
return [
// username and password are both required
['username, password', 'required'],
[['username', 'password'], 'required'],
// password is validated by validatePassword()
['password', 'validatePassword'],
[['password'], 'validatePassword'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
[['rememberMe'], 'boolean'],
];
}

24
apps/advanced/common/models/User.php

@ -104,18 +104,18 @@ class User extends ActiveRecord implements IdentityInterface
public function rules()
{
return [
['username', 'filter', 'filter' => 'trim'],
['username', 'required'],
['username', 'string', 'min' => 2, 'max' => 255],
['email', 'filter', 'filter' => 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'unique', 'message' => 'This email address has already been taken.', 'on' => 'signup'],
['email', 'exist', 'message' => 'There is no user with such email.', 'on' => 'requestPasswordResetToken'],
['password', 'required'],
['password', 'string', 'min' => 6],
[['username'], 'filter', 'filter' => 'trim'],
[['username'], 'required'],
[['username'], 'string', 'min' => 2, 'max' => 255],
[['email'], 'filter', 'filter' => 'trim'],
[['email'], 'required'],
[['email'], 'email'],
[['email'], 'unique', 'message' => 'This email address has already been taken.', 'on' => 'signup'],
[['email'], 'exist', 'message' => 'There is no user with such email.', 'on' => 'requestPasswordResetToken'],
[['password'], 'required'],
[['password'], 'string', 'min' => 6],
];
}

6
apps/advanced/frontend/models/ContactForm.php

@ -23,11 +23,11 @@ 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'],
[['email'], 'email'],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha'],
[['verifyCode'], 'captcha'],
];
}

6
apps/basic/models/ContactForm.php

@ -23,11 +23,11 @@ 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'],
[['email'], 'email'],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha'],
[['verifyCode'], 'captcha'],
];
}

6
apps/basic/models/LoginForm.php

@ -21,11 +21,11 @@ class LoginForm extends Model
{
return [
// username and password are both required
['username, password', 'required'],
[['username', 'password'], 'required'],
// password is validated by validatePassword()
['password', 'validatePassword'],
[['password'], 'validatePassword'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
[['rememberMe'], 'boolean'],
];
}

4
extensions/gii/Generator.php

@ -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'],
];
}

12
extensions/gii/generators/controller/Generator.php

@ -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.'],
]);
}

22
extensions/gii/generators/crud/Generator.php

@ -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'],
]);
}

16
extensions/gii/generators/form/Generator.php

@ -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.'],
]);
}

22
extensions/gii/generators/model/Generator.php

@ -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'],
]);
}

10
extensions/gii/generators/module/Generator.php

@ -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'],
]);
}

6
tests/unit/data/base/Singer.php

@ -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'],
];
}
}

6
tests/unit/data/validators/models/FakedValidationModel.php

@ -28,8 +28,8 @@ class FakedValidationModel extends Model
public function rules()
{
return [
['val_attr_a, val_attr_b', 'required', 'on' => 'reqTest'],
['val_attr_c', 'integer'],
[['val_attr_a', 'val_attr_b'], 'required', 'on' => 'reqTest'],
[['val_attr_c'], 'integer'],
];
}
@ -60,4 +60,4 @@ class FakedValidationModel extends Model
{
return $attr;
}
}
}

Loading…
Cancel
Save