Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Tighten Canadian postal code validation.

D, F, I, O, Q, U should not be valid anywhere in a canadian postal code.

Fixes #3708
  • Loading branch information...
commit 83de70efcf2f4f0f65bb0cf513f7e73927947bea 1 parent 3f94d1d
@markstory markstory authored
View
4 lib/Cake/Test/Case/Utility/ValidationTest.php
@@ -2126,6 +2126,10 @@ public function testPostal() {
$this->assertFalse(Validation::postal('BAA 0ABC', null, 'ca'));
$this->assertFalse(Validation::postal('B2A AABC', null, 'ca'));
$this->assertFalse(Validation::postal('B2A 2AB', null, 'ca'));
+ $this->assertFalse(Validation::postal('K1A 1D1', null, 'ca'));
+ $this->assertFalse(Validation::postal('K1O 1Q1', null, 'ca'));
+ $this->assertFalse(Validation::postal('A1A 1U1', null, 'ca'));
+ $this->assertFalse(Validation::postal('A1F 1B1', null, 'ca'));
$this->assertTrue(Validation::postal('X0A 0A2', null, 'ca'));
$this->assertTrue(Validation::postal('G4V 4C3', null, 'ca'));
View
4 lib/Cake/Utility/Validation.php
@@ -646,7 +646,9 @@ public static function postal($check, $regex = null, $country = 'us') {
$regex = '/\\A\\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\\b\\z/i';
break;
case 'ca':
- $regex = '/\\A\\b[ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9]\\b\\z/i';
+ $district = '[ABCEGHJKLMNPRSTVYX]';
+ $letters = '[ABCEGHJKLMNPRSTVWXYZ]';
+ $regex = "/\\A\\b{$district}[0-9]{$letters} [0-9]{$letters}[0-9]\\b\\z/i";
break;
case 'it':
case 'de':

11 comments on commit 83de70e

@dereuromark
Collaborator

We should probably also update (the redundant) https://github.com/cakephp/localized

@ceeram
Collaborator

maybe all localized validations should be moved to the localized plugin, also i noticed phone() method uses 'can' as country, where postal uses 'ca'

@dereuromark
Collaborator

I agree.

PS: The same "spelling mistake" I noted a few days ago in the core dev channel.

@ADmad
Collaborator

I would also prefer moving all localized stuff into the localized plugin. But we can't break BC in 2.x.

@markstory
Owner

Yeah we started the localized plugin after there were more and more demands for various other locales. That's the main reason the localized hooks exist in the core. I'm fine with extracting the localizations out in a future release. However, which 'postal' implementation is the canonical one? USA is just another locale to me as a non-american.

@dereuromark
Collaborator

like the phone validation there should then be some "all" generic postal code validation regexp allowing "A-Z0-9" and dash + space etc (which would be valid for all countries then).

@markstory
Owner

Except that A-Z0-9 isn't valid in all countries. So the default validation would just be wrong.

@dereuromark
Collaborator

You forgot the missing chars like dash and space [A-Z0-9- ]. Default validation would at least filter always-invalid chars like % and $ etc then.
But I see that this would not be ideal.

@ADmad
Collaborator

Such a generic filter wouldn't be useful to anybody. In such a case perhaps the method could simply act as a stub with actual rules plugged in through the localized plugin.

@dereuromark
Collaborator

And throwing an exception maybe if not properly extended by the localized plugin?

@markstory
Owner

A stub method might work, we'd need to be better about keeping localized up to date though. I haven't looked at it recently and don't know how complete/well tested it is.

Please sign in to comment.
Something went wrong with that request. Please try again.