0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

validatorjs's wanna

Last updated at Posted at 2019-07-06

JSバリデーションを手軽にできる validatorjs ですが、公式ドキュメントに記載しているルール after:date これにハマった。

Available Rules
after:date
The field under validation must be after the given date.

一見、Laravelの after バリデーションルールと同じ形だが、結論から言うとここの date には直接日付文字列は渡せない。
例えばこんな感じ after:2019-01-01after:tomorrow これはダメ。

このafterルールは基本的に同じフォームパラメータと比較するためのものらしい。
例えば、 start_date , end_date という2つのフォームパラメータが存在した際に、下記のように使用する。ちなみにこの after と同じ after_or_equal や before や before_or_equal なども同じ仕様。

let data = {
  start_date: '2019-01-01',
  end_date: '2019-02-01'
};

let rules = {
    start_date: 'required|date',
    end_date: 'required|date|after:start_date',
};

let validation = new Validator(data, rules);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?