Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Add allowSpaces and allowLowerCase option in IBAN/BIC constraints #30272

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Bic.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,23 @@ class Bic extends Constraint
const INVALID_COUNTRY_CODE_ERROR = '1ce76f8d-3c1f-451c-9e62-fe9c3ed486ae';
const INVALID_CASE_ERROR = '11884038-3312-4ae5-9d04-699f782130c7';
const INVALID_IBAN_COUNTRY_CODE_ERROR = '29a2c3bb-587b-4996-b6f5-53081364cea5';
const INVALID_SPACES_ERROR = 'cf0325f4-5c35-47a8-a360-51dac05b6540';

protected static $errorNames = [
self::INVALID_LENGTH_ERROR => 'INVALID_LENGTH_ERROR',
self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
self::INVALID_BANK_CODE_ERROR => 'INVALID_BANK_CODE_ERROR',
self::INVALID_COUNTRY_CODE_ERROR => 'INVALID_COUNTRY_CODE_ERROR',
self::INVALID_CASE_ERROR => 'INVALID_CASE_ERROR',
self::INVALID_SPACES_ERROR => 'INVALID_SPACES_ERROR',
];

public $message = 'This is not a valid Business Identifier Code (BIC).';
public $ibanMessage = 'This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.';
public $iban;
public $ibanPropertyPath;
public $allowLowerCase = false;
public $allowSpaces = true;

public function __construct($options = null)
{
Expand Down
11 changes: 10 additions & 1 deletion src/Symfony/Component/Validator/Constraints/BicValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedValueException($value, 'string');
}

if (!$constraint->allowSpaces && false !== strpos($value, ' ')) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Bic::INVALID_SPACES_ERROR)
->addViolation();

return;
}

$canonicalize = str_replace(' ', '', $value);

// the bic must be either 8 or 11 characters long
Expand Down Expand Up @@ -122,7 +131,7 @@ public function validate($value, Constraint $constraint)
}

// should contain uppercase characters only
if (strtoupper($canonicalize) !== $canonicalize) {
if (!$constraint->allowLowerCase && strtoupper($canonicalize) !== $canonicalize) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Bic::INVALID_CASE_ERROR)
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Iban.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,20 @@ class Iban extends Constraint
const CHECKSUM_FAILED_ERROR = 'b9401321-f9bf-4dcb-83c1-f31094440795';
const INVALID_FORMAT_ERROR = 'c8d318f1-2ecc-41ba-b983-df70d225cf5a';
const NOT_SUPPORTED_COUNTRY_CODE_ERROR = 'e2c259f3-4b46-48e6-b72e-891658158ec8';
const INVALID_CASE_ERROR = 'd58f8108-22b8-4a4b-a151-09b40fa416a6';
const INVALID_SPACES_ERROR = 'cf0325f4-5c35-47a8-a360-51dac05b6540';

protected static $errorNames = [
self::INVALID_COUNTRY_CODE_ERROR => 'INVALID_COUNTRY_CODE_ERROR',
self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
self::CHECKSUM_FAILED_ERROR => 'CHECKSUM_FAILED_ERROR',
self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',
self::NOT_SUPPORTED_COUNTRY_CODE_ERROR => 'NOT_SUPPORTED_COUNTRY_CODE_ERROR',
self::INVALID_CASE_ERROR => 'INVALID_CASE_ERROR',
self::INVALID_SPACES_ERROR => 'INVALID_SPACES_ERROR',
];

public $message = 'This is not a valid International Bank Account Number (IBAN).';
public $allowLowerCase = true;
public $allowSpaces = true;
}
20 changes: 19 additions & 1 deletion src/Symfony/Component/Validator/Constraints/IbanValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ public function validate($value, Constraint $constraint)

$value = (string) $value;

if (!$constraint->allowSpaces && false !== strpos($value, ' ')) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Iban::INVALID_SPACES_ERROR)
->addViolation();

return;
}

if (!$constraint->allowLowerCase && strtoupper($value) !== $value) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Iban::INVALID_CASE_ERROR)
->addViolation();

return;
}

// Remove spaces and convert to uppercase
$canonicalized = str_replace(' ', '', strtoupper($value));

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

// ...have a format available
if (!array_key_exists($countryCode, self::$formats)) {
if (!\array_key_exists($countryCode, self::$formats)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Iban::NOT_SUPPORTED_COUNTRY_CODE_ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,34 @@ public function getValidBicSpecialCases()
yield ['BARCGGSA', 'GB12 CPBK 0892 9965 0449 911'];
yield ['BARCVGSA', 'GB12 CPBK 0892 9965 0449 911'];
}

public function testValidBicAllowLowerCase()
{
$constraint = new Bic([
'allowLowerCase' => true,
]);

$this->validator->validate('DeutAT2LXXX', $constraint);

$this->assertNoViolation();
}

public function testInvalidBicDisallowSpaces()
{
$bic = 'DEUTA T2LX XX';

$constraint = new Bic([
'allowSpaces' => false,
'message' => 'myMessage',
]);

$this->validator->validate($bic, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$bic.'"')
->setCode(Bic::INVALID_SPACES_ERROR)
->assertRaised();
}
}

class BicComparisonTestClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,67 @@ public function getIbansWithInvalidCountryCode()
];
}

public function testValidIbanAllowLowerCase()
{
$iban = 'fr14 2004 1010 0505 0001 3M02 606';

$constraint = new Iban([
'message' => 'myMessage',
'allowLowerCase' => true,
]);

$this->validator->validate($iban, $constraint);

$this->assertNoViolation();
}

public function testInvalidIbanDisallowLowerCase()
{
$iban = 'fr14 2004 1010 0505 0001 3M02 606';

$constraint = new Iban([
'message' => 'myMessage',
'allowLowerCase' => false,
]);

$this->validator->validate($iban, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$iban.'"')
->setCode(Iban::INVALID_CASE_ERROR)
->assertRaised();
}

public function testValidIbanAllowSpaces()
{
$iban = 'FR14 2004 1010 0505 0001 3M02 606';

$constraint = new Iban([
'allowSpaces' => true,
]);

$this->validator->validate($iban, $constraint);

$this->assertNoViolation();
}

public function testInvalidIbanDisallowSpaces()
{
$iban = 'FR14 2004 1010 0505 0001 3M02 606';

$constraint = new Iban([
'message' => 'myMessage',
'allowSpaces' => false,
]);

$this->validator->validate($iban, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$iban.'"')
->setCode(Iban::INVALID_SPACES_ERROR)
->assertRaised();
}

private function assertViolationRaised($iban, $code)
{
$constraint = new Iban([
Expand Down