Reputation: 269
So i have validator that checks if reg_number is unique. I use it for creating new entries (that part works). However, when i try to update an existing one using another form - i get error that reg_number already exists (logical). So my question is how to edit the rule that it would make an exception when i try to update (update other fields and keep reg_number the same) and not create a new one. Found something in other forums (added at the end of the rule) but its not working. Code below:
public function rules()
{
return [
'reg_number'=>'required|max:6|min:6|unique:cars'.$this->cars['reg_number'],
'brand'=>'required',
'model'=>'required'
];
}
Cheers!
Upvotes: 0
Views: 54
Reputation: 17378
There's an example as to how to do this in the Laravel docs.
Basically, you add the ID for the model instance you want to be excepted from this rule. E.g.:
public function rules()
{
return [
'reg_number' => ['required', 'max:6', 'min:6', 'unique:cars,reg_number,' . $this->id],
'brand' => ['required'],
'model' => ['required'],
];
}
Upvotes: 2