Commit 4a02e0b5 by Qiang Xue

finished form generator.

parent 6222e70c
......@@ -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:
*
* ~~~
......
......@@ -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., <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()
{
$output = <<<EOD
$code = highlight_string($this->render($this->getTemplatePath() . '/action.php'), true);
return <<<EOD
<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>
<pre style="background:white">
$code
</pre>
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 {
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();
}
}
<?php
use yii\helpers\Inflector;
/**
* Created by JetBrains PhpStorm.
* User: qiang
* Date: 8/17/13
* Time: 3:48 PM
* To change this template use File | Settings | File Templates.
* This is the template for generating an action view file.
*
* @var yii\base\View $this
* @var yii\gii\generators\form\Generator $generator
*/
?>
<?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,
));
}
<?php
/**
* Created by JetBrains PhpStorm.
* User: qiang
* Date: 8/17/13
* Time: 3:47 PM
* To change this template use File | Settings | File Templates.
* This is the template for generating an action view file.
*
* @var yii\base\View $this
* @var yii\gii\generators\form\Generator $generator
*/
?>
<?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, '-')); ?> -->
......@@ -40,7 +40,7 @@ foreach ($generator->templates as $name => $path) {
Please select which set of the templates should be used to generated the code.
'); ?>
<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 echo Html::submitButton('Generate', array('name' => 'generate', 'class' => 'btn btn-danger')); ?>
......
......@@ -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')),
......
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