0

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

2 回答 2

2

我的兄弟 seeivasan 在 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 论坛

花式盒子

于 2012-12-22T19:40:24.717 回答
0

只需'enableClientValidation'=>true,beginwidget.

于 2012-12-22T11:19:08.063 回答