New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
localized date incorrectly submitted as part of <form> #21
Comments
Parsing is only used when typing into the input itself with a keyboard. All values passed as props should be in ISO-8601 format, not your locale. Is that where you've gone wrong? I'm on my mobile, so this is just a guess :) |
What do you mean with "values passed as props"? The |
I mean even if you are using localisation, you should still pass dates in ISO format:
Can you confirm you're doing this? |
I am indeed doing this when the page is generated on the server initially. The proper value is shown according to the |
OK, I think the issue title threw me off as it seems unrelated to what you're describing. Could you share some of your code? |
Sure. So I have added the js and css in my
The Thymeleaf code that uses the component looks like this:
This renders in the browser like this: You can see that the custom This is the HTML that Chrome dev tools shows: You can see that the If I select a different date using the picker, I can see the When I post the form after that, I get a validation error that the value is not present. If I remove the code to overwrite the The HTML looks the same to me: And posting the form works without issues after selecting a different date (or not selecting any date at all). |
Having done some investigation, the input is being submitted, but it's being submitted with the localised value. e.g. so in this case you'll get a form submission with I think a fix for this would be to have a hidden input (within the component, not for you to add), which would store the current ISO value and participate in form submission. The text input is then purely for the purpose of display/manual entry |
Yes, that would be great. Is this something that could be fixed on short term? I am writing a book about Thymeleaf and wanted to include an example of using a date picker. I'd love to use this one, but I would need a fix for this, preferably in the coming weeks if that is possible? |
I think we should be able to get a fix out this week or next. It won't be a big change on our end. You might be able to work around it with something like this (not tested!!): <duet-date-picker name=""></duet-date-picker>
<input type="hidden" name="birthday" />
<script>
const picker = document.querySelector("duet-date-picker");
const hidden = document.querySelector(`input[type="hidden"]`);
picker.dateAdapter = {
parse(value = "", createDate) {
try {
console.log("parsing" + value);
const matches = value.match(DATE_FORMAT);
if (matches) {
return createDate(matches[3], matches[2], matches[1]);
}
} catch (err) {
console.log(err);
}
},
format(date) {
console.log("formatting: " + date);
return `${date.getDate()}.${date.getMonth() + 1}.${date.getFullYear()}`;
},
};
picker.addEventListener("duetChange", (e) => {
hidden.value = e.detail.value
});
</script> Basically, copy the value on change into a hidden input yourself. But that's a bit of a hack, and is essentially what we'd end up doing behind the scenes |
ok, great. I'll wait for the update. Thanks! |
I'm going to edit the issue title to better reflect the problem, hope that's ok! |
@WickyNilliams Any idea when a fix can be released on this? |
Sorry, this fell through the net. I'll see if i can get a PR together tomorrow |
PR ready here: #43. Feel free to test @wimdeblauwe and let me know if it works for you :) |
Thanks! How can I get the binaries to test? |
Is this included in the 1.1.0 release? Or can I see release notes somewhere? |
Yes, it is included. There's not much by the way of release notes (there is a short changelog in the readme). Using this code as an example, you will now always get an ISO date submitted via the form, despite using a British date adapter (omitted for conciseness): <form>
<duet-date-picker name="date"></duet-date-picker>
<button type="submit">Submit</button>
</form>
<script>
const picker = document.querySelector("duet-date-picker");
picker.dateAdapter = britishDateAdapter
</script> Hope that helps |
I can confirm it works with version 1.1.0. Thanks for the bugfix! |
I am trying to use duet-date-picker with Thymeleaf (server-side rendering). It works as long as I don't use the localization support.
I added this in a
<script>
tag in my page:I see lot's of
formatting...
statements, but neverparsing...
and also no error in the console.When I check with dev tools, I see updates to the
value
attribute of<duet-date-picker>
, but thevalue
of the actual<input>
is not updated. Due to this, my server-side thinks that nothing was selected. For some reason, that does not seem to be an issue when not using the localization support. Am I doing something wrong?The text was updated successfully, but these errors were encountered: