Browse Source

finished form generator.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
4a02e0b539
  1. 5
      framework/yii/gii/Module.php
  2. 51
      framework/yii/gii/generators/form/Generator.php
  3. 28
      framework/yii/gii/generators/form/templates/action.php
  4. 36
      framework/yii/gii/generators/form/templates/form.php
  5. 2
      framework/yii/gii/views/default/view.php
  6. 1
      tests/unit/framework/web/UrlRuleTest.php

5
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 * 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` * the URL `http://localhost/path/to/index.php?r=gii`
* *
* If your application enables [[UrlManager::enablePrettyUrl|pretty URLs]], depending on your * If your application enables [[UrlManager::enablePrettyUrl|pretty URLs]] and you have defined
* configuration, you may need to add the following URL rules in your application configuration * 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: * in order to access Gii:
* *
* ~~~ * ~~~

51
framework/yii/gii/generators/form/Generator.php

@ -21,7 +21,7 @@ class Generator extends \yii\gii\Generator
public $modelClass; public $modelClass;
public $viewPath = '@app/views'; public $viewPath = '@app/views';
public $viewName; public $viewName;
public $scenarioName = 'default'; public $scenarioName;
/** /**
@ -53,20 +53,26 @@ class Generator extends \yii\gii\Generator
return $files; return $files;
} }
/**
* @inheritdoc
*/
public function rules() public function rules()
{ {
return array_merge(parent::rules(), array( 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, viewName, viewPath', 'required'),
array('modelClass, viewPath', 'match', 'pattern' => '/^@?\w+[\\-\\/\w+]*$/', 'message' => 'Only word characters, dashes, slashes and @ are allowed.'), array('modelClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'),
array('viewName', 'match', 'pattern' => '/^\w+[\\-\\/\w+]*$/', 'message' => 'Only word characters, dashes and slashes are allowed.'),
array('modelClass', 'validateModel'), 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('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() public function attributeLabels()
{ {
return array( return array(
@ -77,6 +83,9 @@ class Generator extends \yii\gii\Generator
); );
} }
/**
* @inheritdoc
*/
public function requiredTemplates() public function requiredTemplates()
{ {
return array( return array(
@ -99,20 +108,32 @@ class Generator extends \yii\gii\Generator
public function hints() public function hints()
{ {
return array( return array(
'modelClass' => 'This is the model class for collecting the form input. You should provide a fully qualified class name, e.g., <code>app\models\Post</code>.',
'viewName' => 'This is the view name with respect to the view path. For example, <code>site/index</code> would generate a <code>site/index.php</code> 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., <code>@app/views</code>.',
'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() public function successMessage()
{ {
$output = <<<EOD $code = highlight_string($this->render($this->getTemplatePath() . '/action.php'), true);
return <<<EOD
<p>The form has been generated successfully.</p> <p>The form has been generated successfully.</p>
<p>You may add the following code in an appropriate controller class to invoke the view:</p> <p>You may add the following code in an appropriate controller class to invoke the view:</p>
<pre style="background:white">
$code
</pre>
EOD; EOD;
$code = "<?php\n" . $this->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 { try {
if (class_exists($this->modelClass)) { 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() public function validateViewPath()
{ {
$path = Yii::getAlias($this->viewPath, false); $path = Yii::getAlias($this->viewPath, false);
@ -136,11 +160,16 @@ EOD;
} }
} }
/**
* @return array list of safe attributes of [[modelClass]]
*/
public function getModelAttributes() public function getModelAttributes()
{ {
/** @var Model $model */ /** @var Model $model */
$model = new $this->modelClass; $model = new $this->modelClass;
$model->setScenario($this->scenarioName); if (!empty($this->scenarioName)) {
$model->setScenario($this->scenarioName);
}
return $model->safeAttributes(); return $model->safeAttributes();
} }
} }

28
framework/yii/gii/generators/form/templates/action.php

@ -1,8 +1,26 @@
<?php <?php
use yii\helpers\Inflector;
/** /**
* Created by JetBrains PhpStorm. * This is the template for generating an action view file.
* User: qiang *
* Date: 8/17/13 * @var yii\base\View $this
* Time: 3:48 PM * @var yii\gii\generators\form\Generator $generator
* To change this template use File | Settings | File Templates.
*/ */
?>
<?php echo "<?php\n"; ?>
public function action<?php echo Inflector::id2camel(trim(basename($generator->viewName), '_')); ?>()
{
$model = new <?php echo $generator->modelClass; ?><?php echo empty($generator->scenarioName) ? '' : "(array('scenario' => '{$generator->scenarioName}'))"; ?>;
if ($model->load($_POST)) {
if($model->validate()) {
// form inputs are valid, do something here
return;
}
}
return $this->render('<?php echo $generator->viewName; ?>', array(
'model' => $model,
));
}

36
framework/yii/gii/generators/form/templates/form.php

@ -1,8 +1,34 @@
<?php <?php
/** /**
* Created by JetBrains PhpStorm. * This is the template for generating an action view file.
* User: qiang *
* Date: 8/17/13 * @var yii\base\View $this
* Time: 3:47 PM * @var yii\gii\generators\form\Generator $generator
* To change this template use File | Settings | File Templates.
*/ */
?>
<?php echo "<?php\n"; ?>
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/**
* @var yii\base\View $this
* @var <?php echo $generator->modelClass; ?> $model
* @var ActiveForm $form
*/
<?php echo "?>"; ?>
<div class="<?php echo str_replace('/', '-', trim($generator->viewName, '_')); ?>">
<?php echo '<?php'; ?> $form = ActiveForm::begin(); ?>
<?php foreach ($generator->getModelAttributes() as $attribute): ?>
<?php echo '<?php'; ?> echo $form->field($model, '<?php echo $attribute; ?>'); ?>
<?php endforeach; ?>
<div class="form-group">
<?php echo '<?php'; ?> echo Html::submitButton('Login', array('class' => 'btn btn-primary')); ?>
</div>
<?php echo '<?php'; ?> ActiveForm::end(); ?>
</div><!-- <?php echo str_replace('/', '-', trim($generator->viewName, '-')); ?> -->

2
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. Please select which set of the templates should be used to generated the code.
'); ?> '); ?>
<div class="form-group"> <div class="form-group">
<?php echo Html::submitButton('Preview', array('name' => 'preview', 'class' => 'btn btn-primary')); ?> <?php echo Html::submitButton('Preview', array('name' => 'preview', 'class' => 'btn btn-success')); ?>
<?php if(isset($files)): ?> <?php if(isset($files)): ?>
<?php echo Html::submitButton('Generate', array('name' => 'generate', 'class' => 'btn btn-danger')); ?> <?php echo Html::submitButton('Generate', array('name' => 'generate', 'class' => 'btn btn-danger')); ?>

1
tests/unit/framework/web/UrlRuleTest.php

@ -461,6 +461,7 @@ class UrlRuleTest extends TestCase
), ),
array( array(
array('post/1/a/yes', 'post/index', array('page' => '1', 'tag' => 'a', 'sort' => 'yes')), 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/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/2/a', 'post/index', array('page' => '2', 'tag' => 'a', 'sort' => 'yes')),
array('post/a/no', 'post/index', array('page' => '1', 'tag' => 'a', 'sort' => 'no')), array('post/a/no', 'post/index', array('page' => '1', 'tag' => 'a', 'sort' => 'no')),

Loading…
Cancel
Save