验证表单而不重定向

时间:2012-12-21 23:55:25

标签: php javascript jquery yii fancybox

我用yii创建了应用程序,我需要在Fancy-box中加载一个视图表单 我这样做但我的问题是,当我点击提交按钮时,表单将我重定向到控制器操作而不验证表单。

如何在Fancy-box中没有重定向的情况下验证表单?

我的观点:

<?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来自另一个视图的调用

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

控制器:

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

2 个答案:

答案 0 :(得分:2)

我的兄弟在Yii论坛上解决了这个问题,所以我将在这里分享代码

_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(); ?>

在控制器中

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

参考链接:

Yii Forum

Fancy Box

答案 1 :(得分:0)

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