unique multiple columns validate with AjaxValidation won't work

I want something like this http://www.yiiframework.com/extension/unique-attributes-validator/

as it show in the link




public function rules() {

    return array(

        array('firstKey', 'unique', 'criteria'=>array(

            'condition'=>'`secondKey`=:secondKey',

            'params'=>array(

                ':secondKey'=>$this->secondKey

            )

        )),

    );

}



but when I checked my sql log SELECT 1 FROM account t WHERE (firstKey=‘nakarin’) AND (secondKey=NULL) LIMIT 1

so performAjaxValidation function won’t work(secondKey=NULL).please help me to solve this problem.

The in-built validator isn’t designed for that, but you could try this extension:

http://www.yiiframework.com/extension/unique-multiple-columns-validate/

Failing that, your best bet is probably to create a custom validation method in the model:

http://www.yiiframework.com/doc/guide/1.1/en/form.model#declaring-validation-rules

this won’t work no matter by Ajax or not. rules() are fetched BEFORE values are assigned (they are used to determine which attributes are ‘safe’) and in that point of time attribute has null value.

generally AJAX send all attributes every time, so this is not AJAX validation issue (like I said earlier).

to have multiple column qunique check you have to use extension http://www.yiiframework.com/extension/unique-multiple-columns-validate/

or create validating function and create instance of unique validator there everytime:




public function rules() {

    return array(

        array('firstKey', 'pkIsUnique' ),

    );

}

public function pkIsUnique( $attribute, $params ) {

  CValidator::createValidator( 'unique', $this, $attribute, array( 'criteria'=>array(

            'condition'=>'`secondKey`=:secondKey',

            'params'=>array(

                ':secondKey'=>$this->secondKey

            )

  ) ) )->validate( $this );

}



This should work because $this->secondKey is fetched at validation (AFTER attributes have been assigned)

Thanks for all you guys,http://www.yiiframework.com/extension/unique-multiple-columns-validate/ is OK,but the redguy 's method seem more practical.

Sick man!!! This totally worked!!! Nice catch!