Comments (20)
Excellent! I've just released 0.6.7, this was a good improvement. Thank you for all your help.
from sveltekit-superforms.
Thank you very much for your investigation, I'll do some experimentation, trying to make this consistent!
from sveltekit-superforms.
Yes, already added that. :) It should be fine, so I'll add this for the update tomorrow.
from sveltekit-superforms.
I tested your changes locally and it seems the date input is behaving normally, date comes back to the backend untouched, nice!
from sveltekit-superforms.
I had a use case where I have to deal with datetime in UTC formate in the backed (GraphQL/Postgres) but converted to local datetime in the browser.
I had to implemente DateInput component which is used here
dateProxy looks like correct way to implement this. Looking forward for this solution…
from sveltekit-superforms.
Just confirmed the bug has been fixed!
from sveltekit-superforms.
Another thing I noticed: when using z.date({coerce: true}) fields, on page.server.ts the date is parsed correctly, but on the client-side it applies the local timezone and messes it up
from sveltekit-superforms.
Made some changes to showcase this behavior, notice the server log with the birthdate value and then the same value on the browser: https://stackblitz.com/edit/sveltekit-superforms-bug-reporting-ek13fm?file=src/routes/+page.server.ts
from sveltekit-superforms.
Found the fix: https://stackblitz.com/edit/sveltekit-superforms-bug-reporting-ek13fm?file=src/routes/+page.server.ts
It's two things:
- sveltekit-superforms should add the transform in the example above to any z.date() field
- The function inside the transform to convert date to UTC should also be applied to dateProxy (which just executes new Date(date)), otherwise you get the right date on load but whenever you change the date on the input it messes it up again.
from sveltekit-superforms.
@thelinuxlich Could you clarify what was the problem you had? I've tested with a fork of your project: https://stackblitz.com/edit/sveltekit-superforms-bug-reporting-91avkz
I've commented out the date transform just for testing, then changed dates and submitted several times, but I cannot see that there is a discrepancy between any dates. They are the same both on server and on client.
from sveltekit-superforms.
You need to change the locale/region of your system. For example, I'm in Sao Paulo - Brazil, which is timezone GMT -03:00. On the server the timezone doesn't get applied, it's UTC. On the browser it does, so a UTC date becomes 1 day before because of the -03:00.
from sveltekit-superforms.
I'm at +02:00, but you mean it happens when you are in a negative timezone?
from sveltekit-superforms.
yeah, because at +02:00 a UTC date like 1984-09-02T00:00:00.000Z won't change the day, right?
from sveltekit-superforms.
Yes, that will mess things up because there is only a "date-local" format. I was considering adding a utc format as well, but was hoping it wouldn't be needed. Here's to hoping... :)
When adding that format, it seems to work, though I'm not sure if "date-utc" is semantically correct. It should really mean "use the date part, no matter what timezone", since the date input is a string value, and converting it to date may actually be problematic?
from sveltekit-superforms.
I think date-utc is correct, while you are at it, may add datetime-utc for datetime fields too.
I don't think the conversion would be problematic, the source code for the proxies has a toValue function already doing new Date(val)
from sveltekit-superforms.
I think you'll need to change this line to convert to UTC too when it's "date-utc": https://github.com/ciscoheat/sveltekit-superforms/blob/main/src/lib/client/proxies.ts#L100
Otherwise the z.date() field receives new Date("YYYY-mm-dd") and goes to the server with the local timezone
from sveltekit-superforms.
If you convert it to UTC at that point, which will happen when you change date in the input field for example, it will for negative timezones go backwards, making the input show one day earlier again.
I think the problem is that the date input doesn't care about time more than setting it to midnight at the current timezone. That's why I think describing the format as "date-utc" could be a bit misleading. Maybe a final option is to add just "date", that strips the rest of the ISO string?
from sveltekit-superforms.
Which is why I added the transform()
from sveltekit-superforms.
I have pushed the fixes now to the repo, not released a npm version yet. As it is now, if you instantiate the date as:
const date = new Date('1984-09-02');
And use the date
(or date-utc
) format, it works without any transform:
let proxy = dateProxy(form, 'proxy', { format: 'date' });
Does this sound good, or can you find some case where it will be problem?
from sveltekit-superforms.
if the date remains 1984-09-02 after the date proxy sends the update to the $form field and back to the backend, then yeah it's solved
from sveltekit-superforms.
Related Issues (20)
- Dynamic Default Values? HOT 16
- Installing Superforms Causes Cloudflare Deployment to Fail HOT 7
- New reset-behaviour HOT 18
- Small typo in docs: reference to z despite switching the validation library to Joi HOT 2
- In SPA mode, navigating to a page that triggers a redirect inside `onUpdate` fails silently HOT 1
- TypeError: Cannot read properties of undefined (reading 'defaults') at Module.superValidate HOT 1
- Reverse In and Out type parameter in superForm return type HOT 1
- FormPathLeaves<FormObj, Date> = never HOT 2
- When I build, I am getting a Circular Dependency warning. HOT 5
- using pick on a valibot schema causes superforms to fail to submit when use:enhance is on. HOT 3
- Allow using superForms without a schema HOT 5
- Zod superRefine not updating $errors HOT 5
- zod() Incompatible with z.ZodType HOT 3
- Version 2.6.2 breaks my SvelteKit app, causing infinite loading on localhost HOT 3
- Custom callback for when submission is completed HOT 5
- Form gets cleared after submit. HOT 3
- Error: Unsupported valibot schema: enum HOT 2
- Support for schema references ($ref) HOT 6
- Target attribute not supported on <form> with use:enhance HOT 2
- Superdebug collapsed too much when without label HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sveltekit-superforms.