Validating a fields with multiple required_if conditions.
a laravel validation question. how can I use the
required_if
condition with 2 fields? I am using the rules()
method to define it, but not sure how exactly to do it.
I tried to look at the docs, I came across the when
method on the validation for multiple conditions, but not sure if there is a straight-forward answer to this?12 Replies
->requiredWithAll('field1,field2')
Can I add value conditions to these fields as mentioned above?
A custom rule, I think
ya, I was wondering how I would go about doing it
You may want to do that before you save in your DB
I think there is a function call that intercepts the data in the create file in which you can hook to it and do a custom validation.
Something like this:
Or beforeValidate/afterValidate
https://filamentphp.com/docs/2.x/admin/resources/creating-records#lifecycle-hooks
Filament
Creating records - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
ok, that is a custom validation to check if 2 fields are of the value set. I am trying to validate that
field3
is required if field
is a
and field2
is b
. Where do I set the required
rule for field3
from the above example.
I was wondering if something like would work
or
@awcodes @Dan Harrin sorry to add you into this, but can you help with this when you get a chance. Been struggling for a bit now..required(fn ($get) => …)
damn, that was simple. I did not know
required
accepted a callable. Thank you so much.
Really appreciate the help(mostly) everything accepts a closure
Just now seeing this. Hope you got it sorted.
Hi guys, I have the following :
Forms\Components\Checkbox::make('is_recurrent')
->reactive(),
Forms\Components\TextInput::make('duration')
->required(fn ($get) => $get('is_recurrent') == false)
When I check the is_recurrent box, the red asterix disappear but my browser still ask me to fill the fields do you now why ? thanks