SettingsPage + Toggle + requiredWith validation not working?

When using Filament\Pages\SettingsPage and Filament\Forms\Components\Toggle should I expect ->requiredWith('some_other_field') validation rule to work with the Toggle as a boolean? For me none of the requiredWith/requiredWithout/etc type rules work in this scenario.
19 Replies
FullyLoadedTech
FullyLoadedTechOP2y ago
In the docs it says to cast the model attribute to a boolean, dunno how to do that with Spatie settings as it does not have you create a model. The Toggle alone does however save as boolean already. Trying to think if the Toggle value is validating as '0' as opposed to false or something which might cause requiredWith() to fail.
Adam Holmes
Adam Holmes5mo ago
Did you get anywhere with this? I can't seem to get any required* validation working
awcodes
awcodes5mo ago
Is settings page a custom page?
Adam Holmes
Adam Holmes5mo ago
I sort of hijacked this - I'm not using the settings page. I've just got 2 toggles in the same form (in a standard resource with a standard form) where at least one of the toggles must be switched on. I've tried the following: ->required - just ignored ->requiredIf('other field', false) - i.e. if the other field hasn't been toggled on, this one is required I even tried a custom rule, but I've not got that to hand any more. Everything pointed to toggles just ignoring required* methods
awcodes
awcodes5mo ago
If it has to be one or the other, then why not just one toggle or a radio? Then you wouldn’t have to validate anything.
Adam Holmes
Adam Holmes5mo ago
It's not one or the other - it's at least one of but NOT neither
awcodes
awcodes5mo ago
My bad.
Adam Holmes
Adam Holmes5mo ago
So you can have YES YES YES NO NO YES but not NO NO
awcodes
awcodes5mo ago
Got it. Misread it. I don’t think toggles can be required. They are always going to exist. They’ll never be null. Might need a custom validation rule to get the value of both and fail if they are both false.
Adam Holmes
Adam Holmes5mo ago
I thought I'd got around it by doing this... but it didn't work:
->required(
function (Get $get) {
return $get('other_toggle') !== 1;
}
),
->required(
function (Get $get) {
return $get('other_toggle') !== 1;
}
),
Next step was to do a form rule to check both, but I felt like the require functions must have to work and I was doing something wrong
awcodes
awcodes5mo ago
Are both toggles live() And are they at the same level in the form? Ie not nested in separate layout components.
Adam Holmes
Adam Holmes5mo ago
They're both live They are in separate Fieldsets
awcodes
awcodes5mo ago
Ok. So you’ll need to traverse with the get. $get(‘../fieldset/otherfield’) You have to traverse the state tree relative to where each field is
Adam Holmes
Adam Holmes5mo ago
😮 Is this documented anywhere? Just tried it and no joy 😦
awcodes
awcodes5mo ago
I would expect it to work though. Fieldset may not have an identifier. Try get(‘../../otherfield’). Should just be a matter of getting the path right.
Adam Holmes
Adam Holmes5mo ago
Brill, I'll have another go in the morning and report back. Thanks for your help!
awcodes
awcodes5mo ago
Have a good night.
Adam Holmes
Adam Holmes5mo ago
No joy 😦 I've tried: - Custom validation on the two fields with traversal. - Lots of testing on two new fields that are on the same level - Simple validation on a single toggle field even when checking itself to see if it's on or off, the validation is still ignored i.e. I can save the form even when the toggle is off Managed to get it working like this:
Toggle::make('toggle')
->rules([
fn (Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
if ($value != 1 && $get('other_toggle') != 1) {
$fail('At least one toggle must be selected');
}
},
])
->live(),
Toggle::make('toggle')
->rules([
fn (Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
if ($value != 1 && $get('other_toggle') != 1) {
$fail('At least one toggle must be selected');
}
},
])
->live(),
Good luck to me writing tests for that 😂
Want results from more Discord servers?
Add your server