简体   繁体   中英

actionCreate being called multiple times - Yii

I'm running into a bug where my model is saving multiple times whenever I submit a gii-generated field. I've created an error log so that I can see what is being called multiple times, and I have found out that the function actionCreate is the part of my code that being called three times (Though sometimes two). When I fill out a form an click submit, the error log shows that the actionCreate function is being called three times.

The controller form looks like this 

/**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate()
    {
        $model=new Account;
        error_log("How many times do I call actionCreate");
        // To-Do make the user account creation update via ajax 
        // $this->performAjaxValidation($model);

        if(isset($_POST['Account']))
        {
            $model->attributes=$_POST['Account'];

            if($model->save())
            {
                echo 'do we reach here';
                $this->redirect(array('index','id'=>$model->id));
            }

        }

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

My form, as well, looks like this

<?php
/* @var $this AccountController */
/* @var $model Account */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'account-form',
    // Please note: When you enable ajax validation, make sure the corresponding
    // controller action is handling ajax validation correctly.
    // There is a call to performAjaxValidation() commented in generated controller code.
    // See class documentation of CActiveForm for details on this.
    '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'=>40,'maxlength'=>40)); ?>
        <?php echo $form->error($model,'name'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'mobile_comp'); ?>
        <?php echo CHtml::dropDownList(CHtml::activeName($model,'mobile_comp'), $select, 
               $model->providerOptions, array('empty'=>'(Select Your Provider')); ?>
        <?php echo $form->error($model,'mobile_comp'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'msisdn'); ?>
        <?php echo $form->textField($model,'msisdn'); ?>
        <?php echo $form->error($model,'msisdn'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'pin'); ?>
        <?php echo $form->textField($model,'pin'); ?>
        <?php echo $form->error($model,'pin'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'company'); ?>
        <?php echo $form->textField($model,'company',array('size'=>40,'maxlength'=>40)); ?>
        <?php echo $form->error($model,'company'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'balance'); ?>
        <?php echo $form->textField($model,'balance',array('size'=>40,'maxlength'=>40)); ?>
        <?php echo $form->error($model,'balance'); ?>
    </div>

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

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

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

错误是在我的表单中,我使用ajaxValidation => true而不是ajaxValidation => false导致我意外地用一个字段多次调用了ActionCreate函数。

Becuase you've commented in line:

// To-Do make the user account creation update via ajax 
// $this->performAjaxValidation($model);

Function $this->performAjaxValidation($model); validates form data if requested via Ajax and returns validation results as JSON and stops application execution ahead.

In your case if Ajax validation is happening then there is nothing to check for validation and problem is saving your model.

Just un-comment $this->performAjaxValidation($model);

ALSO: Please add code of function $this->performAjaxValidation($model)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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