Reputation: 381
I.E. If i update address_line1 then its giving error for mobile number allocated. While update it should not match with it self. Even if i change mobile number it should check with other user.
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');
}
}
Thanks in Advance
Upvotes: 0
Views: 779
Reputation: 3893
You should be able to use the unique
validator for this, by simply adding a filter condition like this
public function rules()
{
return [
[['mobile_number','address_line1','address_line2','city','state','country','pincode' ],'required'],
['mobile_number','unique', 'filter' => ['status' => 'A']],
];
}
Upvotes: 0
Reputation: 4160
Change your condition as :-- Override beforeSave()
of 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);
}
Upvotes: 0