I have a problem in yii validation rule, I make a signup form with couponcode for my product. When user enter a coupon code I want to check this couponcode is exist in coupon table or not,but problem is it,I want when user enter couponcode then my validation rule work otherwise if user can’t enter a couponcode this validation rule do not work,for user signup I have a user model and for coupon I have a coupon model, I am using this method in validation rules.
class User extends CActiveRecord
{
public $couponcode;
public function rules(){
.......
array('couponcode', 'isCouponCodeExist'),
}//end rules
public function isCouponCodeExist($attribute, $params)
{
$record = Coupon::model()->findByAttributes(array('couponcode' => $this->couponcode));
if($record === null){
$this->addError($attribute, 'Invalid Coupon');
return false;
}
return true;
}
} //class end
any suggesstion will be helpfull for me