Form builder - general validation error?
Hello π
New to Filament here, working my first real project! π
I'm getting stuck on what seems to be an easy issue, so not sure if I'm getting things wrong, or simply not finding the required info (been on this for 3hrs already π’ ).
What I'm trying to do is generate a global form error, is that possible? In this instance, I'm checking if any of my fields are filled, if not I want to display a validation error (globally).
Looked through
$form->...
couldn't find anything. Then found mutateFormDataBeforeCreate()
on CreateClientContact
but something doesn't feel right, really feel like I'm missing something obvious here?
Hope someone can help me keep a few hairs! π
Thanks, Chris15 Replies
Little confusing, wouldn't you just have the field as required() then the form fails until it is filled? By default there isn't a global error as you want to point to what needs filling?
Thank you for your reply π The situation is I have a random amount of fields, and I need to make sure at least one is filled, so they're not all required. I could be thinking this wrong though, open to suggestions. I was expecting some kind of global error management possibility because errors not linked to specific fields can happen sometimes? (I did just manage something via
->beforeCreate()
sending a notification + ->halt()
which works but, I don't know, seems long winded).So is that in a dynamic repeater field?
No, it's on different forms
Ok how are the random amount of fields set? But one needs to be filled, can you please provide a bit more context?
It might be you group them, and custom validate the group to ensure one must be filled.
Oh, I can custom validate a group? That sounds like a decent solution.
I meant 'random' in the sense I have multiple forms with random fields, one form could be 2 fields (which would be ok with
->requiredWithoutAll(...)
), one could be 6 fields (which seems like a lot, would prefer a single error message than 6 red fields, if that makes sense), etc.
Don't forget I'm new to Filament (although 20 years of PHP, and 5 years of Laravel) so I could be completely missing obvious solutions here πHmm so you would need a custom validation rule looking at it, and on that rule set that one of X fields needs to be field, if one isn't return halt on the rule if that makes sense?
Then you can change the message to be: sorry but atleast 1 of these fields is required
The logic is correct, but how would that message be thrown and where would it appear? I feel like there isn't anything in Filament to manage this? Unless I make a custom view, but that's really getting complicated for not much really.
The initial question was mainly to make sure I hadn't missed anything obvious π Seems like I didn't.
So it woud be as:
https://filamentphp.com/docs/3.x/forms/validation#custom-rules
Then. usign the $get to get the fields are not empty, if they all are then you throw the error
so
But this would still be linked to a single field, right?
Well, yes but the validation would be against all fields. So it would note all the fields and that message shows on each. If that makes sense?
Yes I see what you mean, but I think this doesn't help my case of, when 6 fields, that's a lot of red with a long "one of [...] fields needs filling", so I'm not sure it's the best, I would have preferred a global status message around the form or next to the save button. I think the only way to achieve this would be with custom views. Alternatively, a notification is a bit easier but I think not as clear.
Thanks so much for your help & information, very helpful π
What about using a repeater field or #awcodes-table-repeater ? which then you could set an alter on the field as a whole?
That could be interesting, I'll investigate.