1

I am trying to write validation rule for credit card month and I am using Respect Validation library for this.

v::string()->date('m')->validate('02');

Result is FALSE but it must be TRUE because "02" is valid month

Farid Movsumov
  • 12,350
  • 8
  • 71
  • 97

3 Answers3

1

EDIT: Okay i found problem. Because February have only 28 days you can't verify date with this library without day (right now is 30th day in month, so it returns 03 instead of 02 in comparison).

Soloution:

v::int()->between(1, 12)->validate(02);

OR

you can add first day to comparison.

$value = '01-'.$input;    
v::string()->date('d-m')->validate($value);
3y3skill3r
  • 964
  • 1
  • 11
  • 35
0

You need to specify the format as !m. As noted on PHP manual, if other parts of a date format are omitted the current time is used. Using ! resets the time to UNIX epoch.

This was explained on another answer here.

PS: we will try to fix that on the library itself.

Community
  • 1
  • 1
0

The problem was that Validation was doing this verification using DateTime::createFromFormat().

The PHP manual says:

If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.

So, to avoid problems, a fix has been made and Validation is now using date_parse_from_format() instead.

This error has been fixed on Mar 31th, 2016 (versions 0.8.14, 0.9.8 and 1.0.5) and it's available on 1.1 as well.

You might consider to upgrade your version of the library.