One click/save - many records in DB

Hello.

When I click on submit button yii saves many time. This is my table structure:

  • IDodjel

  • odjel

(Very simple table).

This is how my action for adding new records looks like:


public function actionDodaj	()

	{

		

		$model=new Odjel; 

		 

		if(isset($_POST['Odjel']))

		{	 

			$model->attributes=$_POST['Odjel'];

			if($model->save())

				$this->redirect(array('data'));

		}

		

		$this->render('dodaj',array(

			'model'=>$model,

		));

		

	}

This is view:




<div class="form">


<?php $form = $this->beginWidget('CActiveForm', array(

    'id'=>'odjel-form',

 

    'enableAjaxValidation'=>true,

    'enableClientValidation'=>true,

    'focus'=>array($model,'odjel'),

)); ?>


<?php echo $form->errorSummary($model); ?>


<div class="row">

    <?php echo $form->labelEx($model,'odjel'); ?>

    <?php echo $form->textField($model,'odjel'); ?>

    <?php echo $form->error($model,'odjel'); ?>

</div>




<div class="row submit">

	<?php echo CHtml::submitButton('Snimi'); ?>

</div>

	

	

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


</div>



And this is model:




<?php


/**

 * This is the model class for table "odjel".

 *

 * The followings are the available columns in table 'odjel':

 * @property integer $IDodjel

 * @property string $odjel

 */

class Odjel extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return Odjel the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'odjel';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('odjel', 'required'),

			array('odjel', 'length', 'max'=>45),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('IDodjel, odjel', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'IDodjel' => 'Idodjel',

			'odjel' => 'Odjel',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('IDodjel',$this->IDodjel);

		$criteria->compare('odjel',$this->odjel,true);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}



I’ve detected the problem.

‘enableAjaxValidation’=>true,

when i change this above to false it’s OK. Anyone know why?

I think it saves the model everytime it validates by ajax for some reason. You also forgot: $this->performAjaxValidation($model); in your controller, maybe it solves your problem too.