F
Filament2y ago
Abi

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.
required_if field_1=0 and field_2 = 'x'
required_if field_1=0 and field_2 = 'x'
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
LeandroFerreira
->requiredWithAll('field1,field2')
Abi
AbiOP2y ago
Can I add value conditions to these fields as mentioned above?
LeandroFerreira
A custom rule, I think
Abi
AbiOP2y ago
ya, I was wondering how I would go about doing it
Oxalate
Oxalate2y ago
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.
LeandroFerreira
Something like this:
TextInput::make('field3')->rules([
function ($get) {
return function (string $attribute, $value, Closure $fail) use ($get) {

if ($get('field1') === 'a' && $get('field2') === 'b') {
$fail("The {$attribute} is invalid.");
}
};
},
])
TextInput::make('field3')->rules([
function ($get) {
return function (string $attribute, $value, Closure $fail) use ($get) {

if ($get('field1') === 'a' && $get('field2') === 'b') {
$fail("The {$attribute} is invalid.");
}
};
},
])
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.
Abi
AbiOP2y ago
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
->rules([
Rule::when(fn($get) => ($get('field1') === 'a' && $get('field2') === 'b'), function ($rule) {
return $rule->required();
}),
]),
->rules([
Rule::when(fn($get) => ($get('field1') === 'a' && $get('field2') === 'b'), function ($rule) {
return $rule->required();
}),
]),
or
->rules([
Rule::when(fn($get) => ($get('field1') === 'a' && $get('field2') === 'b'), 'required'),
]),
->rules([
Rule::when(fn($get) => ($get('field1') === 'a' && $get('field2') === 'b'), 'required'),
]),
@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..
Dan Harrin
Dan Harrin2y ago
required(fn ($get) => …)
Abi
AbiOP2y ago
damn, that was simple. I did not know required accepted a callable. Thank you so much. Really appreciate the help
Dan Harrin
Dan Harrin2y ago
(mostly) everything accepts a closure
awcodes
awcodes2y ago
Just now seeing this. Hope you got it sorted.
Gustave
Gustave2y ago
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
Want results from more Discord servers?
Add your server