简体   繁体   English

验证表单而不重定向

[英]Validating a Form without redirect

I created app with yii and I need to load a view form in Fancy-box. 我用yii创建了应用程序,我需要在Fancy-box中加载一个视图表单。
I did that but my problem is when I click on the submit button the form redirects me to the controller action without validating the form. 我这样做但我的问题是,当我点击提交按钮时,表单将我重定向到控制器操作而不验证表单。

How to validate the form without the redirect inside Fancy-box? 如何在没有Fancy-box内的重定向的情况下验证表单?

My view: 我的观点:

<?php
    $config = array( 
);

$this->widget('application.extensions.fancybox.EFancyBox', array(
'target'=>'#getaction',
'config'=>$config,));

echo CHtml::link('Add Section',array('section/create'),array('id'=>'getaction'));
?>

FormView _from from call from another view FormView _from来自另一个视图的调用

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'section-form',
    'enableAjaxValidation'=>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,'name'); ?>
        <?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'name'); ?>
    </div>

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

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

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

Controller : 控制器:

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

    if(isset($_POST['Section']))
    {
        $model->attributes=$_POST['Section'];
        if($model->validate())
        //// Do Som code here 
            $this->redirect(array('view','id'=>$model->id));
    }

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

the problem was fixed by my bro seenivasan on Yii Forum , so I will share the code here 我的兄弟在Yii论坛上解决了这个问题,所以我将在这里分享这些代码

_form.php _form.php这个

<?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'section-form',
        'enableAjaxValidation'=>true,
        //'enableClientValidation'=>true,
        'clientOptions'=>array('validateOnSubmit'=>true), //This is very important
)); ?>

        <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,'name'); ?>
                <?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>64)); ?>
                <?php echo $form->error($model,'name'); ?>
        </div>

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

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

In controller 在控制器中

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

                // Uncomment the following line if AJAX validation is needed
                 $this->performAjaxValidation($model);//You have enabled ajax validation. You have to uncomment this line.

                if(isset($_POST['Section']))
                {
                        $model->attributes=$_POST['Section'];
                        if($model->save())
                                $this->redirect(array('view','id'=>$model->id));
                }

                if(Yii::app()->request->getIsAjaxRequest())
          echo $this->renderPartial('_form',array('model'=>$model),true,true);//This will bring out the view along with its script.

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

Reference link : 参考链接:

Yii Forum Yii论坛

花式盒子

只需在beginwidget内的数组中添加'enableClientValidation'=>true, beginwidget

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

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