Farid Movsumov
Farid Movsumov

Reputation: 12725

Month validation with "Respect Validation"

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

Upvotes: 1

Views: 491

Answers (3)

henriquemoody
henriquemoody

Reputation: 76

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.

Upvotes: 0

Augusto Pascutti
Augusto Pascutti

Reputation: 386

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.

Upvotes: 0

3y3skill3r
3y3skill3r

Reputation: 1026

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);

Upvotes: 1

Related Questions