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
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.
Did you get anywhere with this? I can't seem to get any required* validation working
Is settings page a custom page?
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* methodsIf 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.
It's not one or the other - it's at least one of but NOT neither
My bad.
So you can have
YES YES
YES NO
NO YES
but not NO NO
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.
I thought I'd got around it by doing this... but it didn't work:
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
Are both toggles live()
And are they at the same level in the form? Ie not nested in separate layout components.
They're both live
They are in separate Fieldsets
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
😮
Is this documented anywhere?
Just tried it and no joy 😦
Not where you would expect to find it. https://filamentphp.com/docs/3.x/forms/fields/repeater#using-get-to-access-parent-field-values
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.
Brill, I'll have another go in the morning and report back.
Thanks for your help!
Have a good night.
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:
Good luck to me writing tests for that 😂