0

IE 如果我更新 address_line1 那么它分配的手机号码错误。虽然更新它不应该与它自己匹配。即使我更改手机号码,它也应该与其他用户核对。

public function rules()
    {
        return [
                    [['mobile_number','address_line1','address_line2','city','state','country','pincode' ],'required'],
                    ['mobile_number','mobile_number_allocation_validate'],

        ];
    }

public function mobile_number_allocation_validate($attribute){
        // add custom validation
        $result = User::find()
        ->where('`mobile_number` = "'.$this->$attribute.'" AND 
                `status` = "A" ')->all();
        if(!empty($result)){
            $this->addError($attribute,'Mobile number is allocated to other vehicle');
        }

    }

提前致谢

4

2 回答 2

0

您应该能够为此使用unique验证器,只需添加这样的过滤条件

public function rules()
    {
        return [
            [['mobile_number','address_line1','address_line2','city','state','country','pincode' ],'required'],
            ['mobile_number','unique', 'filter' => ['status' => 'A']],
        ];
    }
于 2015-12-29T05:27:04.400 回答
0

将您的条件更改为:-beforeSave()覆盖User ActiveRecord

/**
 * BeforeSave() check if User ActiveRecord is created with same calculator param
 * if created then return false with model error
 *
 */
public function beforeSave($insert){
    $model = self::find()->where('`mobile_number` = "'.$this->$attribute.'" AND 
            `status` = "A" ')->all();

    if($model !== null && $model->id !== $this->id){
        $this->addError('mobile_number' , 'Mobile number is allocated to other vehicle');
        return false;
    }

    return parent::beforeSave($insert);
}
于 2015-12-24T05:42:43.027 回答