Reputation: 2540
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 -->
Upvotes: 0
Views: 695
Reputation: 2437
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)
Upvotes: 1
Reputation: 2540
The error was in my form where I had ajaxValidation=>true instead of ajaxValidation=>false I was accidentally calling the function ActionCreate multiple times with one field .
Upvotes: 1