Hi,
I have a form which add states data to the states table(code, name, status). I enabled the client side validation and submit the form. But after checking the state table multiple recodes were added instead of one record.
_form.php inside view
<div style="padding-left: 20px" id="box-content">
<div class="form">
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'state-form',
'enableClientValidation'=>true,//
'clientOptions' => array(
'validateOnSubmit' => 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, 'code'); ?>
<?php echo $form->textField($model, 'code', array('class'=>'form-control mystyle', 'style'=>'width: 10%; ', 'size' => 60, 'maxlength' => 2)); ?>
<?php echo $form->error($model, 'code'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'name'); ?>
<?php echo $form->textField($model, 'name', array('class'=>'form-control', 'style'=>'width: 40%', 'size' => 60, 'maxlength' => 100)); ?>
<?php echo $form->error($model, 'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'status'); ?>
<?php echo $form->dropDownList($model, 'status', array(1 => 'Enabled', 0 => 'Disabled'), array('class'=>'form-control', 'style'=>'width: 10%')); ?>
<?php echo $form->error($model, 'status'); ?>
</div>
<br>
<button onclick="$('#state-form').submit();" class="btn btn-xs btn-default">Save</button>
<a class="btn btn-default" onclick="document.location = 'backend.php?r=state/admin'" >Cancel</a>
<?php $this->endWidget(); ?>
</div><!-- form -->
StateController.php
public function actionCreate()
{
$model = new State;
// Uncomment the following line if AJAX validation is needed
//$this->performAjaxValidation($model);
if(isset($_POST['State']))
{
$model->attributes=$_POST['State'];
if($model->save())
{
Yii::app()->user->setFlash('success', "Success: You have modified State!");
$this->redirect(array('state/admin'));
}
}
$this->render('_form',array(
'model'=>$model,
));
}
State.php
public function tableName()
{
return 'state';
}
/**
* @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('code','required'),
array('name','required'),
array('status', 'numerical', 'integerOnly'=>true),
array('code', 'length', 'max'=>2),
array('name', 'length', 'max'=>100),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('state_id, code, name, status', 'safe', 'on'=>'search'),
);
}
It would be very grateful if any one can help on this matter.