Respect / Validation

The most awesome validation engine ever created for PHP
https://respect-validation.readthedocs.io
MIT License
5.79k stars 776 forks source link

IBAN Validation with non-breaking spaces #1431

Closed dennzo closed 7 months ago

dennzo commented 1 year ago

Interesting edge case here.

Sometimes people (also me) like to copy their IBAN from the banking app over into a payment form. In this case the IBAN displayed in banking apps have a non-breaking space instead of a regular one.

So when validating according to the IBAN Rule, the result is of course false, because only normal spaces get replaced. https://github.com/Respect/Validation/blob/e3740860f9217baa123bfe070c8a91dd07de89fa/library/Rules/Iban.php#L113C1-L114C1

We have now made a quick fix to our code just replacing the non-breaking spaces with normal ones. But this is just a temporary fix. str_replace(["\xc2\xa0", "\u00a0"], ' ', $body)

Can we also replace non-breaking spaces instead of only regular spaces? https://www.fileformat.info/info/unicode/char/00A0/index.htm

Maybe we can use str_replace(["\xc2\xa0", "\u00a0", " "], '', $body)

I can also create a pull request to fix this if you like.

henriquemoody commented 7 months ago

Hi @dennzo!

I'm sorry for my delay! I appreciate that you offered to change the rule yourself. Adding those characters might have more of a negative effect. An example would be someone validating that IBAN to insert in the database; they wouldn't want to save the IBAN like that in a database because they'd have issues later using that IBAN to integrate with a third party.

However, you could make that validation by combining call with iban. It would be something like this:

v::stringType()
    ->call(
        static fn($input) => str_replace(["\xc2\xa0", "\u00a0", " "], '', $input),
        v::iban()
    )
    ->assert("DE89\u00a03704 0044 0532 0130 00");

I hope you found your way around it by now, but I hope that also helps you see alternatives to validate other things.