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 be5d85e

Browse filesBrowse files
committed
feature #29641 [Validator] NotBlank: add a new option to allow null values (dunglas)
This PR was squashed before being merged into the 4.3-dev branch (closes #29641). Discussion ---------- [Validator] NotBlank: add a new option to allow null values | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #27876 | License | MIT | Doc PR | todo This PR adds a new option to the `@NotBlank` constraint to allow null values. As described in #27876, this is particularly useful when creating web APIs. Commits ------- 484d22a [Validator] NotBlank: add a new option to allow null values
2 parents dca0975 + 484d22a commit be5d85e
Copy full SHA for be5d85e

File tree

4 files changed

+34
-0
lines changed
Filter options

4 files changed

+34
-0
lines changed

‎src/Symfony/Component/Validator/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/CHANGELOG.md
+1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/NotBlank.php
+2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/NotBlankValidator.php
+5Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php
+26Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.