F
Filamentβ€’16mo ago
torriv

Forms Reactivity, hidden conditions

I have tried for a couple of days now to make a form to register some times. The
DateTimePicker
DateTimePicker
and
DatePicker
DatePicker
are hidden or shown if the user has checked the "all day" checkbox or not. The problem is that i then have to have one field with the
DateTimePicker
DateTimePicker
and one field with the
DatePicker
DatePicker
with the same field name. I didn't manage to make that work, so what i did is making those fields with different names, and add some hidden fields with the actual field names that are used in the DB and pug $set in those fake name fields in the afterStateUpdated. I managed to make it work on the create process. The problem now is on the edit page. since my fields have fake names in the schema, it won't pick the data from the DB. Do anyone have a suggestion how to solve this? Here is the stripped version of the code: https://gist.github.com/torriv83/e9bfd0bd4a219e0e1f2f5cf656f78d5a
Gist
forms.php
GitHub Gist: instantly share code, notes, and snippets.
Solution:
I finally manged to do it. i had to name the other two fields differently, and then i used mutateFormDataBeforeFill to fill those two. and now it works. Thanks for the help tho!
Jump to solution
22 Replies
torriv
torrivOPβ€’16mo ago
Anyone had a look? πŸ™‚ i'm still stuck on this.
Thijmen
Thijmenβ€’16mo ago
Are you only using the form builder package?
torriv
torrivOPβ€’16mo ago
no, i'm using the panel
Thijmen
Thijmenβ€’16mo ago
Is filling the edit page not working or saving?
torriv
torrivOPβ€’16mo ago
when i When i'm editing, the times won't be prefilled everything else in the form is filled correctly.
Thijmen
Thijmenβ€’16mo ago
You could try with the beforeFill() method
Thijmen
Thijmenβ€’16mo ago
I also don't quite understand why you have problems with multiple components with the same name?
torriv
torrivOPβ€’16mo ago
i'll try. thanks. the only thing i try to do is show the datetimepicker if allday is not checked, and the datepicker if allday is checked.
Thijmen
Thijmenβ€’16mo ago
But if you remove all extra components what is the problem?
torriv
torrivOPβ€’16mo ago
i'm not sure if i follow you. what do you mean?
Thijmen
Thijmenβ€’16mo ago
Well what doesn't work if you just only keep the datetimepicker and the datepicker fields So not the duplicate fields with fake names
torriv
torrivOPβ€’16mo ago
so, i removed the hidden fields, and named the other fields to the actual name they need to have. in the creation process, everything works. in the edit, i get this error in my console: e specified value "2023-08-22" does not conform to the required format. The format is "yyyy-MM-ddThh:mm" followed by optional ":ss" or ":ss.SSS". b
torriv
torrivOPβ€’16mo ago
and the times does not get filled
torriv
torrivOPβ€’16mo ago
if i remove the datepicker fields, it works... so i guess thats why I tried to name the fields differently.
Thijmen
Thijmenβ€’16mo ago
So your datetimepicker and datepicker field have the same name?
torriv
torrivOPβ€’16mo ago
yes. they are saved in the database like this: 2023-08-22 14:00:00 or if all day: 2023-08-22 00:00:00 and i also have a column in my database called allDay that has either 0 or 1
Thijmen
Thijmenβ€’16mo ago
You could have only that two fields with the same name. And use the beforeCreate, beforeFill, beforeSave to alter the data
torriv
torrivOPβ€’16mo ago
i have never used those hooks before, so i'm not sure how to use them to solve my issue. do you have some examples to give me? the documentation did not give me much examples.
Thijmen
Thijmenβ€’16mo ago
I think there are a lot of examples in this discord Just search on those words
josef
josefβ€’16mo ago
You could also use mutators to fill the shown fields with the data from the actual field when getting the model
Solution
torriv
torrivβ€’16mo ago
I finally manged to do it. i had to name the other two fields differently, and then i used mutateFormDataBeforeFill to fill those two. and now it works. Thanks for the help tho!
Want results from more Discord servers?
Add your server