Skip to content

Commit

Permalink
Ignore null values, so it can be handled by another constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Nov 9, 2019
1 parent 94b4e53 commit c134efc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Validator/Constraints/NipValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ class NipValidator extends ConstraintValidator
{
/**
* {@inheritDoc}
*
* @var Nip $constraint
*/
public function validate($value, Constraint $constraint)
{
if (null === $value) {
return;
}

if (!preg_match($pattern = $this->getPattern($constraint), $value)) {
$this->context->buildViolation($constraint->patternMessage)
->setParameter('{{ value }}', $this->formatValue($value))
Expand Down
6 changes: 6 additions & 0 deletions tests/Validator/Constraints/NipValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,12 @@ public function testInvalidCustomPrefixLength($value)
->assertRaised();
}

public function testNullValueIsIgnored()
{
$this->validator->validate(null, new Nip());
$this->assertNoViolation();
}

public function testNipConstraintClassAlias()
{
$this->assertInstanceOf(Nip::class, new Tin());
Expand Down

0 comments on commit c134efc

Please sign in to comment.