Livewire $rules

What seems to be the problem:
I have a Livewire Class that has a defined set of validation rules.

public function rules()
    {
        return [
            'editing.description' => 'required|unique:methods,description',
            'editing.method_category_id' => 'required',
            'editing.method' => 'required'
        ];
    }

I use the component to create and edit method statements, so how do I only check for unique when creating a new method? Currently I can not save an updated method unless I change the description.

Use the ignore method

https://laravel.com/docs/8.x/validation#rule-unique (read the bit under “Forcing A Unique Rule To Ignore A Given ID”)

Validator::make($data, [
    'email' => [
        'required',
        Rule::unique('users')->ignore($user->id),
    ],
]);

That’s exactly what I was looking for. Thanks.

1 Like