Skip to content
Open
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
3 changes: 3 additions & 0 deletions Constraints/NotBlankValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/**
* @author Bernhard Schussek <[email protected]>
* @author Kévin Dunglas <[email protected]>
* @author Lionel Kimb's <[email protected]>
*/
class NotBlankValidator extends ConstraintValidator
{
Expand All @@ -35,6 +36,8 @@ public function validate(mixed $value, Constraint $constraint): void
$value = ($constraint->normalizer)($value);
}

$value = is_string($value) ? trim($value) : $value;

if (false === $value || (empty($value) && '0' != $value)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
Expand Down
14 changes: 14 additions & 0 deletions Tests/Constraints/NotBlankValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ public function testBlankIsInvalid()
->assertRaised();
}

public function testBlankTextIsInvalid()
{
$constraint = new NotBlank([
'message' => 'myMessage',
]);

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

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '""')
->setCode(NotBlank::IS_BLANK_ERROR)
->assertRaised();
}

public function testFalseIsInvalid()
{
$constraint = new NotBlank([
Expand Down