Making-Websites-With-October-CMS icon indicating copy to clipboard operation
Making-Websites-With-October-CMS copied to clipboard

Problem to format date before save

Open gabrielGenesis opened this issue 6 years ago • 3 comments

I extended the user plugin just like the video, but in my october a create a extra field that is a 'date' field. Everything works fine, but i need to format the date before save it and then format again to show it in front and backend.

My code is just like the video with that extra field that works without the formatation that i need.

Here in Brazil we use dates like 'dd/mm/yyyy' so I want to take the date 'dd/mm/yyyy' from frontend, change it to 'yyyy-dd-mm' save it and then, when I need to retreave it (front end backend) i show it like 'dd/mm/yyyy' again.

gabrielGenesis avatar Jan 14 '19 17:01 gabrielGenesis

To be honest I don't exactly know how to integrate beforeSave function in extended model - https://octobercms.com/docs/api/model/beforesave

But what you can do is to use JS date picker plugin for your form. They usually allow you to choose the format that the user sees and the format that is actually going to be sent to the DB.

And then you can use twig to convert the date from DB to format that you need when displaying that date on the frontend: https://twig.symfony.com/doc/2.x/filters/date.html

Or another solution might be to just save the date in date field the way it is, since you created custom date field. This should work very easily. But it's not a good solution if you actually need to calculate the dates for some reason. Although you can convert the date to the format you need when doing the calculation.

Hope this helps you or at least points you in the right direction.

ivandoric avatar Jan 15 '19 13:01 ivandoric

Thank you very much, i will try it.

Do you know if I can extend the 'register' function, maybe copy and past the original one and then change the field before save it? I know your sugestion will work, but i'm curious to know if is there other options in case that i need it in the future.

Thanks again.

gabrielGenesis avatar Jan 15 '19 15:01 gabrielGenesis

As I've said I currently don't know how to do this the "October" way. But I'm sure there is a way, I just don't have the time right now to try to find it.

I would suggest you start with this - https://octobercms.com/docs/plugin/extending - although it seems to me you won't find your answer there, and then maybe ask at October forums etc.

ivandoric avatar Jan 16 '19 08:01 ivandoric