Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e9c8b35

Browse filesBrowse files
committed
[Validator] Add option allowLowerCase and allowSpaces in IBAN constraint
1 parent cd8c7e8 commit e9c8b35
Copy full SHA for e9c8b35

File tree

3 files changed

+57
-0
lines changed
Filter options

3 files changed

+57
-0
lines changed

‎src/Symfony/Component/Validator/Constraints/Iban.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/Iban.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,20 @@ class Iban extends Constraint
2828
const CHECKSUM_FAILED_ERROR = 'b9401321-f9bf-4dcb-83c1-f31094440795';
2929
const INVALID_FORMAT_ERROR = 'c8d318f1-2ecc-41ba-b983-df70d225cf5a';
3030
const NOT_SUPPORTED_COUNTRY_CODE_ERROR = 'e2c259f3-4b46-48e6-b72e-891658158ec8';
31+
const INVALID_CASE_ERROR = 'd58f8108-22b8-4a4b-a151-09b40fa416a6';
32+
const INVALID_SPACES_ERROR = 'cf0325f4-5c35-47a8-a360-51dac05b6540';
3133

3234
protected static $errorNames = [
3335
self::INVALID_COUNTRY_CODE_ERROR => 'INVALID_COUNTRY_CODE_ERROR',
3436
self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
3537
self::CHECKSUM_FAILED_ERROR => 'CHECKSUM_FAILED_ERROR',
3638
self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',
3739
self::NOT_SUPPORTED_COUNTRY_CODE_ERROR => 'NOT_SUPPORTED_COUNTRY_CODE_ERROR',
40+
self::INVALID_CASE_ERROR => 'INVALID_CASE_ERROR',
41+
self::INVALID_SPACES_ERROR => 'INVALID_SPACES_ERROR',
3842
];
3943

4044
public $message = 'This is not a valid International Bank Account Number (IBAN).';
45+
public $allowLowerCase = true;
46+
public $allowSpaces = true;
4147
}

‎src/Symfony/Component/Validator/Constraints/IbanValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/IbanValidator.php
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,24 @@ public function validate($value, Constraint $constraint)
156156

157157
$value = (string) $value;
158158

159+
if(!$constraint->allowSpaces && preg_match('/\s/', $value)){
160+
$this->context->buildViolation($constraint->message)
161+
->setParameter('{{ value }}', $this->formatValue($value))
162+
->setCode(Iban::INVALID_SPACES_ERROR)
163+
->addViolation();
164+
165+
return;
166+
}
167+
168+
if (!$constraint->allowLowerCase && strtoupper($value) !== $value) {
169+
$this->context->buildViolation($constraint->message)
170+
->setParameter('{{ value }}', $this->formatValue($value))
171+
->setCode(Iban::INVALID_CASE_ERROR)
172+
->addViolation();
173+
174+
return;
175+
}
176+
159177
// Remove spaces and convert to uppercase
160178
$canonicalized = str_replace(' ', '', strtoupper($value));
161179

‎src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function getValidIbans()
5151
return [
5252
['CH9300762011623852957'], // Switzerland without spaces
5353
['CH93 0076 2011 6238 5295 7'], // Switzerland with multiple spaces
54+
['ch93 0076 2011 6238 5295 7'], // Switzerland in lower case
5455

5556
// Country list
5657
// http://www.rbs.co.uk/corporate/international/g0/guide-to-international-business/regulatory-information/iban/iban-example.ashx
@@ -432,6 +433,38 @@ public function getIbansWithInvalidCountryCode()
432433
];
433434
}
434435

436+
public function testInvalidIbanDisallowLowerCase(){
437+
$iban = "fr14 2004 1010 0505 0001 3M02 606";
438+
439+
$constraint = new Iban([
440+
'message' => 'myMessage',
441+
'allowLowerCase' => false,
442+
]);
443+
444+
$this->validator->validate($iban, $constraint);
445+
446+
$this->buildViolation('myMessage')
447+
->setParameter('{{ value }}', '"'.$iban.'"')
448+
->setCode(Iban::INVALID_CASE_ERROR)
449+
->assertRaised();
450+
}
451+
452+
public function testInvalidIbanDisallowSpaces(){
453+
$iban = "FR14 2004 1010 0505 0001 3M02 605";
454+
455+
$constraint = new Iban([
456+
'message' => 'myMessage',
457+
'allowSpaces' => false,
458+
]);
459+
460+
$this->validator->validate($iban, $constraint);
461+
462+
$this->buildViolation('myMessage')
463+
->setParameter('{{ value }}', '"'.$iban.'"')
464+
->setCode(Iban::INVALID_SPACES_ERROR)
465+
->assertRaised();
466+
}
467+
435468
private function assertViolationRaised($iban, $code)
436469
{
437470
$constraint = new Iban([

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.