Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kirc0de opened this issue Oct 7, 2021 · 3 comments
Open

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

kirc0de opened this issue Oct 7, 2021 · 3 comments

Comments

@kirc0de
Copy link

kirc0de commented Oct 7, 2021

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
Copy link
Author

kirc0de commented Oct 7, 2021

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
Copy link
Author

kirc0de commented Oct 7, 2021

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
Copy link

skeemer commented Dec 11, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants