Filamentphp unique ignore during editing

Filamentphp unique ignore during editing

Posted on:March 2, 2024 at 01:00 PM

In the form builder of filamentphp, there is a rule called unique() which is basically confirm the creating / editing record should be unique.

The problem is that, when you have existing record and you want to edit, you might get error because uniqueness will check in the database and get the same record exists.

For example:

TextInput::make('email')
    ->required()
     ->unique(),

You will get Email is already taken validation error.

How to solve?

To solve this, you need to pass ignorable in the unique() method.

  TextInput::make('email')
    ->required()
-   ->unique()
+   ->unique(ignorable: function($record) {
+        return $record;
+    }),

Now it should not validate the email uniqueness when editing an existing record.

Thanks for reading.