简体   繁体   English

在yii中进行错误验证后,留在同一页面上

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

I would like to avoid redirecting after error validation. 我想避免错误验证后的重定向。

I display a list of post and for each post the user can give a comment after clicking on a button. 我显示了一个帖子列表,用户可以在单击按钮后针对每个帖子发表评论。

This display the comment form under the choosen post. 这将在所选帖子下显示评论表单。

If there is an error in validation of the comment I want to stay on the page and display errors beside the field in error (as default). 如果验证验证时出错,我想保留在页面上并在错误字段旁边显示错误(默认)。

Here is My controller action for the comment 这是我的评论员的操作

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,
        ));
    }

and the view 和视图

<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 --> 

For the moment, with Yii::app()->end(); 目前,使用Yii :: app()-> end(); it shows a blank page, if I dont do nothing in the else part , then it continues and displays the create view (loosing some decoration) 它显示了一个空白页,如果我在else部分中什么也不做,则它继续并显示创建视图(失去一些装饰)

---Adding some more information ---添加更多信息

Actually, what I have in my page is this 实际上,我的页面上有这个

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

Post 2

Post 3

when I click on save without comment, I want to stay in this page, giving the user the possibility to see where the error is (comment is missing) and save it again. 当我单击不带注释的保存时,我想停留在此页面上,使用户可以查看错误出在哪里(缺少注释)并再次保存。

Can you tell me where is my mistake? 你能告诉我我的错误在哪里吗?

Thank you for your help 谢谢您的帮助

in the else block you have to tell yii to render the same page again. 在else块中,您必须告诉yii再次渲染同一页面。 After calling $model->save() you get errors back in the model. 调用$ model-> save()后,您会在模型中收到错误。 So before Yii::app()->end(); 所以在Yii :: app()-> end();之前 call rendering of your page $this->render('create',array('model'=>$model,)); 调用页面渲染$ this-> render('create',array('model'=> $ model,));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM