簡體   English   中英

在yii中進行錯誤驗證后,留在同一頁面上

[英]stay on the same page after error validation in yii

我想避免錯誤驗證后的重定向。

我顯示了一個帖子列表,用戶可以在單擊按鈕后針對每個帖子發表評論。

這將在所選帖子下顯示評論表單。

如果驗證驗證時出錯,我想保留在頁面上並在錯誤字段旁邊顯示錯誤(默認)。

這是我的評論員的操作

public function actionCreate()
    {
        $model=new Comment;

        if(isset($_POST['Comment']))
        {
            $model->attributes=$_POST['Comment'];

            if($model->save()) {
                $this->redirect(Yii::app()->request->urlReferrer);

            } else {

                 Yii::app()->end();

            }
        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }

和視圖

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'comment-form',
    'enableClientValidation'=>true,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model,'comment'); ?>
        <?php echo $form->textField($model,'comment',array('size'=>60,'maxlength'=>140)); ?>
        <?php echo $form->error($model,'comment'); ?>
    </div>

    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form --> 

目前,使用Yii :: app()-> end(); 它顯示了一個空白頁,如果我在else部分中什么也不做,則它繼續並顯示創建視圖(失去一些裝飾)

---添加更多信息

實際上,我的頁面上有這個

Post 1
(Comment form)
Commment  :   ........
Save

Post 2

Post 3

當我單擊不帶注釋的保存時,我想停留在此頁面上,使用戶可以查看錯誤出在哪里(缺少注釋)並再次保存。

你能告訴我我的錯誤在哪里嗎?

謝謝您的幫助

在else塊中,您必須告訴yii再次渲染同一頁面。 調用$ model-> save()后,您會在模型中收到錯誤。 所以在Yii :: app()-> end();之前 調用頁面渲染$ this-> render('create',array('model'=> $ model,));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM