laravel-validation-rules / credit-card

Credit Card Validation
https://laravel-validation-rules.github.io/
Apache License 2.0
227 stars 69 forks source link

There is no way to set `MM/YY` format for CardExpirationDate #44

Open kirc0de opened 3 years ago

kirc0de commented 3 years ago

I'm trying to do like this:

return [
    'card.expirationDate' => ['required', new CardExpirationDate('MM\/YY')],
    // ...
];

When the lib tun passes() method it runs inside and throw an Exception:

// This can throw Invalid Date Exception if format is not supported.
Carbon::parse($value);

There are no ways to disable this parsing or etc.

What do you think about this bug/feature? :–)

kirc0de commented 3 years ago

The same problem with - instead of /.

So I tried to change test case:

public function it_checks_expiration_date()
{
    // ...
    $this->assertTrue($this->dateValidator('02-18', 'MM-YY')->passes());
    // ...
}

And it fails :–(.

This 02-18 value and MM-YY format are from examples in README.md, but this do not work.

kirc0de commented 3 years ago

I found the source of my problems: there is incorrect format. The right one is Carbon::createFromFormat('m/y', '06/23'). Need to fix it in README.md and change MM-YY format there.

I can fix readme. But what to do with Carbon::parse() and its exception? What do you think?

skeemer commented 2 years ago

I just ran into this issue because I wanted to use the 'm/Y' format. The Carbon::parse() doesn't do anything helpful. I ended up having to abandon this validation for regex/date_format/after validations.

I think that line was added because it blocks a string like '3/22' without a leading zero because there is no way to enforce checking for the leading 0 with the date parsing in PHP. The only way I can thing to fix that is by doing a string comparison of the string to be parsed to the result formatted back. Unfortunately, then formats that you want to have match leading 0 not required but allowed wouldn't be reliable.