douben7260 2015-05-28 15:11
浏览 48
已采纳

在Yii中启用客户端验证和提交表单后添加双记录

I have a following simple form in Yii that is submitted by AJAX:

<?php $form = $this->beginWidget('CActiveForm', array(
        'id' => 'application-form',
        'enableAjaxValidation' => true,
        'enableClientValidation' => true,
        'htmlOptions' => array(
            'enctype' => 'multipart/form-data',
            'onsubmit'=>"return send();"
        ),
        'clientOptions'=>array('validateOnSubmit'=>true)

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

And function for submitting:

function send (){

var data_form=$("#application-form").serialize();

$.ajax({
    type: "POST",
    url: "<?php echo Yii::app()->createUrl('admin/application/create') ?>",
    dataType:'json',
    data: data_form,
    success: function (data, textStatus, jqXHR) {
        console.log(data);
        alert("success",textStatus);
    },
    error: function (jqXHR, textStatus, errorThrown) {
       // console.log( errorThrown);

    }
});
return false;
}

My create controller:

public function actionCreate()
    {
        $model = new Application;
        $model->setScenario('create');

       $this->performAjaxValidation($model);

        if (isset($_POST['Application'])) {

           if ( $model->save()){

                echo CJSON::encode(array('success' => 'true','id'=>$model->id));
                Yii::app()->end();
            }
        }

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

Filed name is required and that is only validation for now. When I try to submit form without field name inputted, validation messages appear as they supposed to do in Yii. But when I fill form correctly, my model is inputted twice in database. If I remove following property:

'clientOptions'=>array('validateOnSubmit'=>true)

model gets saved correctly (only one time), but no validation messages appear.

How can I get default validation messages in Yii to appear, when I submit my form via ajax and model not to be saved twice. I need to submit form this way, because I will return model id in Ajax response for processing in JavaScript.

I searched the web for this and tried all suggestions, but none of them work.

Thank you all!

  • 写回答

1条回答 默认 最新

  • doufan2541 2015-06-05 12:29
    关注

    I solved by adding 'afterValidate'=>'js:function(form,data,hasError){ send(form,data,hasError); and removing 'onsubmit'=>"return send();" line. Now it shows validation errors and only saves model once.

    Check this post for more info.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波