当ajaxvalidation为true时重复插入

时间:2014-12-10 02:31:57

标签: yii

我有这个奇怪的问题导致我的模型在我的表单启用ajaxvalidation时双重插入数据库。我不明白为什么会发生这种情况,但这与我的自定义验证相冲突,我无法继续前进。有人可以启发我的问题。

型号:

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('category, ppmp_id', 'numerical', 'integerOnly'=>true),
        array('category','required'),
        array('q1, q2, q3, q4', 'numerical'),
        array('category', 'myTestUniqueMethod'),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('item_id, category, q1, q2, q3, q4, ppmp_id', 'safe', 'on'=>'search'),
    );
}

public function myTestUniqueMethod($attribute,$params)
{
    $sql = 'SELECT * 
            FROM ppmp_item 
            WHERE ppmp_id='.$this->ppmp_id.'and category='.$this->category;

        $unique = Yii::app()->db->createCommand($sql)->queryRow();

    if ($unique)
    {
            $this->addError('category', "Category already added to PPMP");
    }

}

查看:

 <?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'ppmp-item-form',
'enableAjaxValidation'=>true,
 )); ?>


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

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

        $data=PhilgepsCategory::model()->findAll();
        $data=CHtml::listData($data,'id','class_name');
        echo $form->dropDownList($model,'category',$data,array('options' => array('1'=>array('selected'=>true)))); 

    ?>
    <?php echo $form->error($model,'category'); ?>
</div>

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

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

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

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

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

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

控制器:

public function actionCreate($ppmp,$bal)
{
    $model=new PpmpItem;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['PpmpItem']))
    {
        $model->attributes=$_POST['PpmpItem'];
        $model->ppmp_id = $ppmp;
        $valid=$model->validate();
        if($valid) 
            if($model->save())
                $this->redirect(array('ppmp/view','id'=>$ppmp));
    }

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

1 个答案:

答案 0 :(得分:0)

问题是你在验证时不应该执行 save()方法,因为它会在执行ajax验证时以及通过表单发送数据时保存,因此,你应该做这样的事情:

...
...
if(isset($_POST['ajax']) && $_POST['ajax']==='ppmp-item-form')
{
    echo CActiveForm::validate($model);
    Yii::app()->end();
}

$valid=$model->validate();
     if($valid) 
        if($model->save())
...
...

这与 gii 用于使用ajax验证相同。