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 2c61afe

Browse filesBrowse files
committed
[Validator] Add option allowLowerCase and allowSpaces in IBAN constraint
1 parent 81dbc7e commit 2c61afe
Copy full SHA for 2c61afe

File tree

3 files changed

+86
-1
lines changed
Filter options

3 files changed

+86
-1
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
+19-1Lines changed: 19 additions & 1 deletion
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 && false !== strpos($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

@@ -182,7 +200,7 @@ public function validate($value, Constraint $constraint)
182200
}
183201

184202
// ...have a format available
185-
if (!array_key_exists($countryCode, self::$formats)) {
203+
if (!\array_key_exists($countryCode, self::$formats)) {
186204
$this->context->buildViolation($constraint->message)
187205
->setParameter('{{ value }}', $this->formatValue($value))
188206
->setCode(Iban::NOT_SUPPORTED_COUNTRY_CODE_ERROR)

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/IbanValidatorTest.php
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,67 @@ public function getIbansWithInvalidCountryCode()
432432
];
433433
}
434434

435+
public function testValidIbanAllowLowerCase()
436+
{
437+
$iban = 'fr14 2004 1010 0505 0001 3M02 606';
438+
439+
$constraint = new Iban([
440+
'message' => 'myMessage',
441+
'allowLowerCase' => true,
442+
]);
443+
444+
$this->validator->validate($iban, $constraint);
445+
446+
$this->assertNoViolation();
447+
}
448+
449+
public function testInvalidIbanDisallowLowerCase()
450+
{
451+
$iban = 'fr14 2004 1010 0505 0001 3M02 606';
452+
453+
$constraint = new Iban([
454+
'message' => 'myMessage',
455+
'allowLowerCase' => false,
456+
]);
457+
458+
$this->validator->validate($iban, $constraint);
459+
460+
$this->buildViolation('myMessage')
461+
->setParameter('{{ value }}', '"'.$iban.'"')
462+
->setCode(Iban::INVALID_CASE_ERROR)
463+
->assertRaised();
464+
}
465+
466+
public function testValidIbanAllowSpaces()
467+
{
468+
$iban = 'FR14 2004 1010 0505 0001 3M02 606';
469+
470+
$constraint = new Iban([
471+
'allowSpaces' => true,
472+
]);
473+
474+
$this->validator->validate($iban, $constraint);
475+
476+
$this->assertNoViolation();
477+
}
478+
479+
public function testInvalidIbanDisallowSpaces()
480+
{
481+
$iban = 'FR14 2004 1010 0505 0001 3M02 606';
482+
483+
$constraint = new Iban([
484+
'message' => 'myMessage',
485+
'allowSpaces' => false,
486+
]);
487+
488+
$this->validator->validate($iban, $constraint);
489+
490+
$this->buildViolation('myMessage')
491+
->setParameter('{{ value }}', '"'.$iban.'"')
492+
->setCode(Iban::INVALID_SPACES_ERROR)
493+
->assertRaised();
494+
}
495+
435496
private function assertViolationRaised($iban, $code)
436497
{
437498
$constraint = new Iban([

0 commit comments

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