提交按钮后的Yii2 Basic清除输入表单

时间:2018-01-11 15:10:52

标签: forms yii2 yii2-basic-app

我创建了一个用于创建帖子的模式和另一个用于在另一个视图中创建图库的模式并且它工作得很完美但是在我单击提交之后我的表单保持数据输入即使我刷新页面它仍然输入数据,提交后是否可以清除表格

我的帖子代码是:

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $ly_addPost app\models\Posts */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="posts-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($ly_addPost, 'Post_title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($ly_addPost, 'Post_text')->textarea(['rows' => 6]) ?>

    <?= $form->field($ly_addPost, 'Post_file')->textInput(['maxlength' => true]) ?>

    <?= $form->field($ly_addPost, 'Permission_id')->dropdownList([$ly_addPost->Permission_id]);?>

    <div class="form-group">
        <?= Html::submitButton('Create' , ['class' => 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

我的控制器有两个不同的视图,一个用于发布,另一个用于图库

我的控制器代码是:

public function actionView($id)
    {

        $ly_addPost = new Posts();
        $ly_addGallery = new Galleries();
        //$ly_addAudio = new Audios();
        //$ly_addVideo = new Videos();

        $ly_addPost->Channel_id = $id;
        $ly_addGallery->Channel_id = $id;

        $ly_addPost->Userid = Yii::$app->user->id;
        $ly_addGallery->Userid = Yii::$app->user->id;

        // for permission post
        $ly_addPost->Permission_id = Permission::find()
        ->select(['Permission_type'])
        ->indexBy('Permission_id')
        ->column();

        // for permission galery
        $ly_addGallery->Permission_id = Permission::find()
        ->select(['Permission_type'])
        ->indexBy('Permission_id')
        ->column();

        if ($ly_addPost->load(Yii::$app->request->post()) ) {   

            $ly_addPost->Post_id = Yii::$app->params['ly_randCttid'];
            $ly_addPost->Post_uid = Yii::$app->params['ly_randCttid'];

            $ly_addPost->save();    

            return $this->render('view', [
                'model' => $this->findModel($id),
                'ly_addPost' => $ly_addPost,
                'ly_addGallery' => $ly_addGallery,
            ]);

            exit();
        }
        else if ($ly_addGallery->load(Yii::$app->request->post()) ) {   

            $ly_addGallery_id = Yii::$app->params['ly_randCttid'];
            $ly_addGallery_uid = Yii::$app->params['ly_randCttid'];

            $ly_addGallery->save(); 

            return $this->render('view', [
                'model' => $this->findModel($id),
                'ly_addPost' => $ly_addPost,
                'ly_addGallery' => $ly_addGallery,
            ]);

            exit();
        } else {
        return $this->render('view', [
            'model' => $this->findModel($id),
            'ly_addPost' => $ly_addPost,
            'ly_addGallery' => $ly_addGallery,
        ]);
        }   
    }

2 个答案:

答案 0 :(得分:1)

你必须在渲染之前清除$ ly_addPost

} else {


    foreach ($ly_addPost as $key => $value) {
        $ly_addPost->$key = null;  //set to null instead of unsetting
    } // this foreach clear all variable of   $ly_addPost;

        return $this->render('view', [
            'model' => $this->findModel($id),
            'ly_addPost' => $ly_addPost,
            'ly_addGallery' => $ly_addGallery,
        ]);
}   

答案 1 :(得分:0)

我修改了

return $this->render('view', [
                'model' => $this->findModel($id),
                'ly_addPost' => $ly_addPost,
                'ly_addGallery' => $ly_addGallery,
            ]);

            exit();

return $this->refresh();

现在我在控制器中的代码是

if ($ly_addPost->load(Yii::$app->request->post()) ) {   

            $ly_addPost->Post_id = Yii::$app->params['ly_randCttid'];
            $ly_addPost->Post_uid = Yii::$app->params['ly_randCttid'];

            $ly_addPost->save();    

            return $this->refresh();
        }