Change state before dehydration

For context, i have a DateTimePicker component for selecting the event's start date. If the user marks the all_day checkbox, i need the component to set the hour to 00:00:00 but keeping the date. eg: 28/07/2023 -> 28/07/2023 00:00:00. The thing is: I know i can do this with ->dehydrateStateUsing() but this will override the default dehydration process, which i need because it handles the timezone stuff behind the scenes.. How can i mutate the state before the dehydration process?
Solution:
Filament already have a ->beforeStateDehydrated()
Jump to solution
17 Replies
Dan Harrin
Dan Harrin2y ago
you could do it in mutateFormDataBeforeSave(), right?
Saade
SaadeOP2y ago
yeahh, but i then i need to copy the timezone handling to this method 1. the user selects 14:00 in the frontend 2. the data come as 11:00 (GMT -3 to UTC) 3. i mutate the data to 00:00 (GMT -3) 4. then i would need to shift the timezone again to UTC (21:00) the 4th step is already part of the component dehydration process, if i could mutate the data before the dehydration process it would be easier and DRY i need something like parent::dehydrateStateUsing($state) hahaha
awcodes
awcodes2y ago
Wouldn’t this automatically get handled at the laravel or db level unless your server isn’t UTC.
Saade
SaadeOP2y ago
my laravel app and db are UTC, and my frontend component is ->timezone($user->timezone)
awcodes
awcodes2y ago
I would think it would all just work if your make the field type datetime-local. Let the browser worry about the conversion. Then you only have to worry about converting it if the display is outside on an input.
Saade
SaadeOP2y ago
hmm, i don't get it if the user selects 15:00 in the frontend, the DateTimePicker component will consider that this time is in UTC by default, and save 15:00 in the database which is wrong.. i need the ->timezone($user>timezone) part to the Filament dehydration process to apply -3 hours to that time also, if my record is 12:00 (UTC), does the browser knows it is UTC and apply +3 to that time?
awcodes
awcodes2y ago
I could be wrong but I’m under the impression that datetime-local basically masks the value based on the users browser settings for their timezone. Meaning anything they select would compensate the UTC offset, if im correct.
awcodes
awcodes2y ago
👍
Saade
SaadeOP2y ago
wait
Solution
Saade
Saade2y ago
Filament already have a ->beforeStateDehydrated()
Saade
SaadeOP2y ago
🤦‍♂️ 🤦‍♂️ 🤦‍♂️ 🤦‍♂️
awcodes
awcodes2y ago
Lol.
Saade
SaadeOP2y ago
goddammit
awcodes
awcodes2y ago
Sorry for wasting you time. I’m new to this. 😂
Saade
SaadeOP2y ago
well, VSCode autocompletion sucks, no surprises here...
awcodes
awcodes2y ago
Yes it does.

Did you find this page helpful?