Add DateTime to transformValue in forms
See original GitHub issueWhen working with forms it is quite a nuisance when dealing with Dates. Currently, Prisma only accepts dates as ISO strings. If you use the <DatetimeLocalField />
from the form package you will receive an error by default.
Expected type "DateTime". DateTime cannot represent an invalid date-time-string 2020-11-19T14:50.
You eventually have to transform it yourself before submitting that data to be stored.
new Date(data.date).toISOString())
I believe the solution is simply to add DateTime
to the COERCION_FUNCTIONS
in the forms package
https://github.com/redwoodjs/redwood/blob/6aa9a28f5f3db8d69aced7cab4eb3880734ae6b7/packages/forms/src/coercion.tsx#L24-L29
Also maybe the DateTimeField
could automatically convert the incoming ISO string to a format that the input can accept
const date = "2020-11-12T22:08:00.000Z"
// using date-fn-timezone here to help convert since it is store as iso in prisma
const convertToDateTimeLocale = (date) => {
return formatToTimeZone(date, 'YYYY-M-DTHH:mm:ss.SSS', {
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
})
}
convertToDateTimeLocale(date)
// expected output = 2020-11-12T22:08:00
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Hmm, I could have sworn that using the
<DatetimeLocalField>
would output the proper format for GraphQL/Prisma and that it serialized it back to a string properly to pre-fill the form later. It’s possible that sometime between whenever this was written and 2.16 of Prisma that something changed somewhere?If someone could PR a fix and show it working we’d happily accept it!
@thedavidprice @cannikin I can circle back on this one. I posted this I think back in November. I’ll look to get a PR up here soon. I didn’t see my mentions on this until today.