Records getting saved twice

I have a model and when a new record is created for the model, it gets stored twice instead of a single record.

Below is the code.

Controller


public function actionSubmit()

	{

		$model=new Post;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


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

		{

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

			if($model->save())

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

		}


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

			'model'=>$model

		));

	}

Model


public function rules()

	{

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

		// will receive user inputs.

		return array(

			array('gender, category_id, content', 'required'),

			array('category_id', 'numerical', 'integerOnly'=>true),

			array('gender', 'length', 'max'=>6),

			array('content', 'length', 'max'=>3000),

			array('modified','default',

              'value'=>new CDbExpression('NOW()'),

              'setOnEmpty'=>false,'on'=>'update'),

        	array('created,modified','default',

              'value'=>new CDbExpression('NOW()'),

              'setOnEmpty'=>false,'on'=>'insert'),

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

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

			array('gender, category_id, content, user_id, published, created, modified', 'safe', 'on'=>'search'),

		);

	}

...

...

...

	protected function beforeSave()

	{

	    if(parent::beforeSave())

	    {

	        if($this->isNewRecord)

	        {

	            $this->user_id=Yii::app()->user->id;

	            $this->content = $this->content . ' FML';

	        }

	        return true;

	    }

	    else

	        return false;

	}

View (submit.php)


<div class="well">

<h2>Submit Your Post</h2>


<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>

</div>

View (_form.php)


<div class="form">


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

	'id'=>'post-form',

	'enableAjaxValidation'=>true,

));?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


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


	<div class="row">

		<div class="control-group">

			<div class="controls docs-input-sizes">

				<div class="pull-left">

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

					<?php echo $form->dropDownList($model,'gender',$model->listGenderOptions()); ?>

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

				</div>

				<div class="pull-right">

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

					<?php echo $form->dropDownList($model,'category_id', Category::model()->listCategoryOptions()); ?>

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

				</div>

			</div>

		</div>

	</div>


	<div class="row">

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

		<?php echo $form->textArea($model,'content',array('row'=>5,'size'=>60,'maxlength'=>1024, 'style'=>'width:98.5%;height:100px;')); ?>

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

	</div>


	<div class="row">	

		<div class="pull-right">

			<?php echo CHtml::submitButton('Submit!', array('class'=>'btn-large btn-primary')); ?>

		</div>

	</div>


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


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

You have to do the performAjaxValidation when you have enabled ajax validation in the active form.




	// Uncomment the following line if AJAX validation is needed

	$this->performAjaxValidation($model);



Thanks! That was the problem indeed. It worked after enabling ajax validation in controller.