[1.0.0-alpha.3] Datepicker: When you select a date, the value is not inserted in the input tag
Expected Behavior
When selecting a date, the value attribute of the input tag should be filled.
Current Behavior
The value attribute of the input tag does not fill out.
Steps to Reproduce (for bugs)
I initialize several components on the page, and none of them returns a value. I also tried to leave only one component on the page. Exactly the same behavior is observed on the page http://next.materializecss.com/pickers.html.
My code:
let elem = document.querySelector('.datepicker');
M.Datepicker.init(elem, options);
Your Environment
- Version used: 1.0.0-alpha.3
- Framework: VueJS framework 2.5.13
- Browser Name and version: Irrelevant
- Operating System and version (desktop or mobile): Irrelevant
The value attribute is for default initial value
@Dogfalo The form sends data to the onsubmit event. How do I get the value of a form if the component does not return a value automatically?
Part of my code in VueJS component:
<template>
<form class="row" @submit="store">
<div class="input-field col s6">
<input placeholder="Placeholder" id="first_name" type="text" class="validate" v-model="formData.firstName">
<label for="first_name">First Name</label>
</div>
<div class="col s6">
<input type="text" id="start_date" class="datepicker" v-model="formData.startDate">
<label for="start_date">Start Date</label>
</div>
<div class="col s6">
<button class="btn waves-effect waves-light" type="submit">send</button>
</div>
</form>
</template>
<script type="text/javascript">
data() {
return {
url: {
store: '/orders/store'
},
formData: {}
}
},
mounted() {
let elem = document.querySelector('.datepicker');
M.Datepicker.init(elem);
},
methods: {
store(e) {
e.preventDefault();
axios.post(this.url.store, this.formData)
.then(response => {
// ...
})
.catch(error => {
// ...
})
}
// ...
}
</script>
Is it really necessary to store the instance of the datepicker component and before accessing the form, contact it?
Your inputs are missing the name attribute
I'm using the VueJS framework. If you use the v-model method, you do not need to transfer a name: https://vuejs.org/v2/api/#v-model and https://vuejs.org/v2/guide/forms.html
v-model will only update on the input event so the 2 way binding will not work in the picker. You can listen to the change event instead and set the value on the model to fix this
In general, when the value of the value attribute changes, v-model updates the value of the corresponding variable inside the framework.
Datepicker does not change the value of value for the element, hence, VueJS can not track this.
It seems that the solution to the problem will not be easy, since there can be several datepicker elements on one page.
If you do not take into account the JS-framework, the same behavior of the element is observed on your site: http://next.materializecss.com/pickers.html

It turns out that when you submit the form, you will need to catch additional fields with a date and get their values, substituting in the sent data, as you said.
I don't see vue's example exhibiting that behavior either: https://vuejs.org/v2/guide/forms.html
This is it: https://vuejs.org/v2/guide/forms.html#Text
The input there doesn't have its value attribute changed either
just used v-model.lazy to get datepicker value. Cheers! :)
@aungmyohlaing v-model.lazy works. Do you know the reason behind this behaviour? Thanks for the solution though.
@aungmyohlaing v-model.lazy works. Do you know the reason behind this behaviour? Thanks for the solution though.
"By default, v-model syncs the input with the data after each input event ... You can add the lazy modifier to instead sync after change events"
Thank you for the tip. Vue Docs on Forms