In the codepen below, I have a simple input that contains a date:
<input type="text"
v-model="startDate"
name="StartDate"
v-validate="{ required: false, date_format: 'dd/MM/yyyy', before: maxStartDate }"/>
No matter what valid date I enter, an error is generated:
The StartDate must be before 2019-08-01T03:59:59.999Z.
I've also tried:
I also noticed that vee-validate is using UK-style locales in its messaging. For instance, I'm in the US but dates come back like 25/07/2019. I wonder if date comparisons are somehow being skewed.
The before
validator does not work how you think it does. What it wants is a reference to another field that contains a date - imagine if you were making a startDate and endDate field, you'd want to make sure that startDate is before endDate. That's what this does.
The validator you should be using is date_between
, like so:
<input type="text"
v-model="startDate"
name="StartDate"
v-validate="{ date_format: 'dd/MM/yyyy', date_between:['01/01/1990',maxStartDate] }"/>
And I had to modify how maxStartDate is defined like this:
maxStartDate: function() {
return moment()
.startOf('month')
.add(1, 'months')
.endOf('month').format('DD/MM/YYYY');
}
Working example: https://codepen.io/anon/pen/NZaLzg
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.