'sometimes|exists:foobar,id'
Docs: https://laravel.com/docs/8.x/validation#validating-when-present
Always use ConvertEmptyStringsToNull middleware.
Assuming I have a form with this select tag:
<select name="foobar">
<option value="">Select:</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
I want this to be optionally empty (i.e. the first option), or to validate that the value selected exists in the database. But this validation option doesn't work:
'nullable|exists:foobar,id'
It will validate any selected option ok, but not the top (empty) option, I guess because an empty string is not null. I know there's a ConvertEmptyStringsToNull middleware but I'd rather not activate it if possible. What's the best practice here?
For anyone else encountering this problem, it seems it's a "feature" of Livewire:
Please or to participate in this conversation.