最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using vue.js and vee-validate, date validation using after: and a computed field doesn't seem to work - Sta

programmeradmin2浏览0评论

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:

  • Making maxStartDate return JavaScript date objects, ISO dates (seen here), and simple dates like '1/1/2019'.
  • Various binations of validation rules. No date_format, no required, etc.
  • I have been on this one for hours. Feels like I'm missing something obvious and will soon kick myself.

I also noticed that vee-validate is using UK-style locales in its messaging. For instance, I'm in the US but dates e back like 25/07/2019. I wonder if date parisons are somehow being skewed.

Codepen:

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:

  • Making maxStartDate return JavaScript date objects, ISO dates (seen here), and simple dates like '1/1/2019'.
  • Various binations of validation rules. No date_format, no required, etc.
  • I have been on this one for hours. Feels like I'm missing something obvious and will soon kick myself.

I also noticed that vee-validate is using UK-style locales in its messaging. For instance, I'm in the US but dates e back like 25/07/2019. I wonder if date parisons are somehow being skewed.

Codepen: https://codepen.io/Kinetiq/pen/XLeEaM

Share Improve this question asked Jun 26, 2019 at 18:15 Brian MacKayBrian MacKay 32k17 gold badges93 silver badges126 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论