-
Notifications
You must be signed in to change notification settings - Fork 159
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
Display format vs value format #167
Comments
It's impossible to do it with datepicker alone. You need to separate the data for UI and the one to submit, and sync them using your program, like this: <div class="field">
<label for="testdate">Test date</label>
<input type="hidden" name="testdate" value="2023-10-15">
<input type="text" class="input date" data-value-field="testdate">
</div> var dateInput = document.querySelector('.date');
var valueField = document.querySelector(`input[name="${dateInput.dataset.valueField}"]`);
dateInput.value = valueField.value.replace(/^(\d{4})-(\d\d)-(\d\d)$/, '$3/$2/$1');
var datepicker = new Datepicker(dateInput, {format: 'dd/mm/yyyy'});
dateInput.addEventListener('changeDate', () => {
valueField.value = datepicker.getDate('yyyy-mm-dd') || '';
}); If the app submits data with AJAX, modifying the date format when submitting can be an option. For example, <div class="field">
<label for="testdate">Test date</label>
<input type="text" class="input date" name="testdate" value="15/10/2023">
</div> var dateInput = document.querySelector('.date');
var datepicker = new Datepicker(dateInput, {format: 'dd/mm/yyyy'});
// ...
function submit(formElem) {
const formData = new FormData(formElem);
formData.set('testdate', datepicker.getDate('yyyy-mm-dd'));
return fetch(`/test?${new URLSearchParams(formData).toString()}`);
} For the former, creating a subclass may make things easier, like this: |
Thanks @mymth that worked like a charm! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi.
I'm finding some trouble to configure the plugin to have a display format that's different from the value format.
I'd live to have "dd/mm/yyyy" displaying on the form input but I need the form to actually send "yyyy-mm-dd" to the backend.
I think I've followed the documentation, the plugin displays the format that I need but it also sends the same format when submitting the form.
Is it possible to let me know what I am doing wrong or if something is missing?
Thanks!
The text was updated successfully, but these errors were encountered: