Unique rule not working on update
Hello,
I have this unique rule:
However when updating the record i also get the error message that it is not unique.
This shouldn't be the case. How can I make this rule only validate on creating records and not on updating records?
Solution:Jump to solution
I figured it out. I had extra code in the creating and updating hooks off my model which interfered with the validation logic
7 Replies
Does anyone have an idea on this? Or has anyone dealtt with this issue before?
This should work
With this rule I am still able to create duplicate products
when editing the form then $ref is
null
You want the item code to be unique across all products? Check how unique works at https://laravel.com/docs/12.x/validation#rule-unique
Validation - Laravel 12.x - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
When editing just feed in the current product id
https://filamentphp.com/docs/3.x/forms/validation#unique is an even better read, check the ignoreRecord part in case you use the field in a panel.
Solution
I figured it out. I had extra code in the creating and updating hooks off my model which interfered with the validation logic