I have a livewire component that updates a model in the database. Name and icon must be unique so previously in Laravel I pass in the ID of the model in the validation rule.
This still works if I put the rules inside the $this->validate(); method but not when I define the validation $rules property, since it needs to be a constant property.
But I need my validation to be inside the $rules property because I’m using model binding! Also its much clearer to define them this way.
But how do I write the rules to work when updating unique values? Or do I need to write a custom validation method to achieve this?
class MarkEdit extends Component
{
public Mark $mark;
// Dosent work
protected $rules = [
'mark.name' => 'required|string|min:2|unique:marks,name,' . $this->mark->id,
'mark.icon' => 'required|string|unique:marks,icon,' . $this->mark->id,
'mark.tooltip' => 'nullable|string|max:512',
'mark.color' => 'nullable|string',
];
// etc..
}