diff --git a/framework/yii/gii/Module.php b/framework/yii/gii/Module.php index 9c70de6..f82e3dd 100644 --- a/framework/yii/gii/Module.php +++ b/framework/yii/gii/Module.php @@ -34,8 +34,9 @@ use yii\web\HttpException; * With the above configuration, you will be able to access GiiModule in your browser using * the URL `http://localhost/path/to/index.php?r=gii` * - * If your application enables [[UrlManager::enablePrettyUrl|pretty URLs]], depending on your - * configuration, you may need to add the following URL rules in your application configuration + * If your application enables [[UrlManager::enablePrettyUrl|pretty URLs]] and you have defined + * custom URL rules or enabled [[UrlManager::enableStrictParsing], you may need to add + * the following URL rules at the beginning of your URL rule set in your application configuration * in order to access Gii: * * ~~~ diff --git a/framework/yii/gii/generators/form/Generator.php b/framework/yii/gii/generators/form/Generator.php index 759978c..e02a98e 100644 --- a/framework/yii/gii/generators/form/Generator.php +++ b/framework/yii/gii/generators/form/Generator.php @@ -21,7 +21,7 @@ class Generator extends \yii\gii\Generator public $modelClass; public $viewPath = '@app/views'; public $viewName; - public $scenarioName = 'default'; + public $scenarioName; /** @@ -53,20 +53,26 @@ class Generator extends \yii\gii\Generator return $files; } - + /** + * @inheritdoc + */ public function rules() { return array_merge(parent::rules(), array( - array('modelClass, viewName, scenarioName', 'filter', 'filter' => 'trim'), + array('modelClass, viewName, scenarioName, viewPath', 'filter', 'filter' => 'trim'), array('modelClass, viewName, viewPath', 'required'), - array('modelClass, viewPath', 'match', 'pattern' => '/^@?\w+[\\-\\/\w+]*$/', 'message' => 'Only word characters, dashes, slashes and @ are allowed.'), - array('viewName', 'match', 'pattern' => '/^\w+[\\-\\/\w+]*$/', 'message' => 'Only word characters, dashes and slashes are allowed.'), + array('modelClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'), array('modelClass', 'validateModel'), + array('viewName', 'match', 'pattern' => '/^\w+[\\-\\/\w]*$/', 'message' => 'Only word characters, dashes and slashes are allowed.'), + array('viewPath', 'match', 'pattern' => '/^@?\w+[\\-\\/\w]*$/', 'message' => 'Only word characters, dashes, slashes and @ are allowed.'), array('viewPath', 'validateViewPath'), - array('scenarioName', 'match', 'pattern' => '/^\w+$/', 'message' => 'Only word characters are allowed.'), + array('scenarioName', 'match', 'pattern' => '/^[\w\\-]+$/', 'message' => 'Only word characters and dashes are allowed.'), )); } + /** + * @inheritdoc + */ public function attributeLabels() { return array( @@ -77,6 +83,9 @@ class Generator extends \yii\gii\Generator ); } + /** + * @inheritdoc + */ public function requiredTemplates() { return array( @@ -99,20 +108,32 @@ class Generator extends \yii\gii\Generator public function hints() { return array( + 'modelClass' => 'This is the model class for collecting the form input. You should provide a fully qualified class name, e.g., app\models\Post.', + 'viewName' => 'This is the view name with respect to the view path. For example, site/index would generate a site/index.php view file under the view path.', + 'viewPath' => 'This is the root view path to keep the generated view files. You may provide either a directory or a path alias, e.g., @app/views.', + 'scenarioName' => 'This is the scenario to be used by the model when collecting the form input. If empty, the default scenario will be used.', ); } + /** + * @inheritdoc + */ public function successMessage() { - $output = <<render($this->getTemplatePath() . '/action.php'), true); + return <<The form has been generated successfully.

You may add the following code in an appropriate controller class to invoke the view:

+
+$code
+
EOD; - $code = "render($this->getTemplatePath() . '/action.php'); - return $output . highlight_string($code, true); } - public function validateModel($attribute, $params) + /** + * Validates the model class to make sure it exists and is valid. + */ + public function validateModel() { try { if (class_exists($this->modelClass)) { @@ -128,6 +149,9 @@ EOD; } } + /** + * Validates [[viewPath]] to make sure it is a valid path or path alias and exists. + */ public function validateViewPath() { $path = Yii::getAlias($this->viewPath, false); @@ -136,11 +160,16 @@ EOD; } } + /** + * @return array list of safe attributes of [[modelClass]] + */ public function getModelAttributes() { /** @var Model $model */ $model = new $this->modelClass; - $model->setScenario($this->scenarioName); + if (!empty($this->scenarioName)) { + $model->setScenario($this->scenarioName); + } return $model->safeAttributes(); } } diff --git a/framework/yii/gii/generators/form/templates/action.php b/framework/yii/gii/generators/form/templates/action.php index 68168de..3884b02 100644 --- a/framework/yii/gii/generators/form/templates/action.php +++ b/framework/yii/gii/generators/form/templates/action.php @@ -1,8 +1,26 @@ + +public function actionviewName), '_')); ?>() +{ + $model = new modelClass; ?>scenarioName) ? '' : "(array('scenario' => '{$generator->scenarioName}'))"; ?>; + + if ($model->load($_POST)) { + if($model->validate()) { + // form inputs are valid, do something here + return; + } + } + return $this->render('viewName; ?>', array( + 'model' => $model, + )); +} diff --git a/framework/yii/gii/generators/form/templates/form.php b/framework/yii/gii/generators/form/templates/form.php index 60c9aa3..53cf5e6 100644 --- a/framework/yii/gii/generators/form/templates/form.php +++ b/framework/yii/gii/generators/form/templates/form.php @@ -1,8 +1,34 @@ + + +use yii\helpers\Html; +use yii\widgets\ActiveForm; + +/** + * @var yii\base\View $this + * @var modelClass; ?> $model + * @var ActiveForm $form + */ +"; ?> + +
+ + $form = ActiveForm::begin(); ?> + + getModelAttributes() as $attribute): ?> + echo $form->field($model, ''); ?> + + +
+ echo Html::submitButton('Login', array('class' => 'btn btn-primary')); ?> +
+ ActiveForm::end(); ?> + +
diff --git a/framework/yii/gii/views/default/view.php b/framework/yii/gii/views/default/view.php index b8ff95c..50a87fc 100644 --- a/framework/yii/gii/views/default/view.php +++ b/framework/yii/gii/views/default/view.php @@ -40,7 +40,7 @@ foreach ($generator->templates as $name => $path) { Please select which set of the templates should be used to generated the code. '); ?>
- 'preview', 'class' => 'btn btn-primary')); ?> + 'preview', 'class' => 'btn btn-success')); ?> 'generate', 'class' => 'btn btn-danger')); ?> diff --git a/tests/unit/framework/web/UrlRuleTest.php b/tests/unit/framework/web/UrlRuleTest.php index 3f1c070..d67dc58 100644 --- a/tests/unit/framework/web/UrlRuleTest.php +++ b/tests/unit/framework/web/UrlRuleTest.php @@ -461,6 +461,7 @@ class UrlRuleTest extends TestCase ), array( array('post/1/a/yes', 'post/index', array('page' => '1', 'tag' => 'a', 'sort' => 'yes')), + array('post/1/a/no', 'post/index', array('page' => '1', 'tag' => 'a', 'sort' => 'no')), array('post/2/a/no', 'post/index', array('page' => '2', 'tag' => 'a', 'sort' => 'no')), array('post/2/a', 'post/index', array('page' => '2', 'tag' => 'a', 'sort' => 'yes')), array('post/a/no', 'post/index', array('page' => '1', 'tag' => 'a', 'sort' => 'no')),