8000 [Validator] NotBlank: add a new option to allow null values · symfony/symfony@484d22a · GitHub
[go: up one dir, main page]

Skip to content

Commit 484d22a

Browse files
dunglasfabpot
authored andcommitted
[Validator] NotBlank: add a new option to allow null values
1 parent 98bc3e7 commit 484d22a

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added options `iban` and `ibanPropertyPath` to Bic constraint
88
* added UATP cards support to `CardSchemeValidator`
9+
* added option `allowNull` to NotBlank constraint
910

1011
4.2.0
1112
-----

src/Symfony/Component/Validator/Constraints/NotBlank.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
1919
*
2020
* @author Bernhard Schussek <bschussek@gmail.com>
21+
* @author Kévin Dunglas <dunglas@gmail.com>
2122
*/
2223
class NotBlank extends Constraint
2324
{
@@ -28,4 +29,5 @@ class NotBlank extends Constraint
2829
];
2930

3031
public $message = 'This value should not be blank.';
32+
public $allowNull = false;
3133
}

src/Symfony/Component/Validator/Constraints/NotBlankValidator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
/**
1919
* @author Bernhard Schussek <bschussek@gmail.com>
20+
* @author Kévin Dunglas <dunglas@gmail.com>
2021
*/
2122
class NotBlankValidator extends ConstraintValidator
2223
{
@@ -29,6 +30,10 @@ public function validate($value, Constraint $constraint)
2930
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\NotBlank');
3031
}
3132

33+
if ($constraint->allowNull && null === $value) {
34+
return;
35+
}
36+
3237
if (false === $value || (empty($value) && '0' != $value)) {
3338
$this->context->buildViolation($constraint->message)
3439
->setParameter('{{ value }}', $this->formatValue($value))

src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,30 @@ public function testEmptyArrayIsInvalid()
9898
->setCode(NotBlank::IS_BLANK_ERROR)
9999
->assertRaised();
100100
}
101+
102+
public function testAllowNullTrue()
103+
{
104+
$constraint = new NotBlank([
105+
'message' => 'myMessage',
106+
'allowNull' => true,
107+
]);
108+
109+
$this->validator->validate(null, $constraint);
110+
$this->assertNoViolation();
111+
}
112+
113+
public function testAllowNullFalse()
114+
{
115+
$constraint = new NotBlank([
116+
'message' => 'myMessage',
117+
'allowNull' => false,
118+
]);
119+
120+
$this->validator->validate(null, $constraint);
121+
122+
$this->buildViolation('myMessage')
123+
->setParameter('{{ value }}', 'null')
124+
->setCode(NotBlank::IS_BLANK_ERROR)
125+
->assertRaised();
126+
}
101127
}

0 commit comments

Comments
 (0)
0