8000 [Validator] Add "trim" option to the NotBlankValidator · symfony/symfony@7faf0e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7faf0e2

Browse files
committed
[Validator] Add "trim" option to the NotBlankValidator
1 parent 7378a5f commit 7faf0e2

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,15 @@ public function validate($value, Constraint $constraint)
3838
}
3939

4040
$stringValue = (string) $value;
41-
42-
$stringValue = $constraint->ltrim ? ltrim($stringValue) : $stringValue;
43-
$stringValue = $constraint->rtrim ? rtrim($stringValue) : $stringValue;
44-
45-
if ($constraint->mergeConsecutiveWhitespaces) {
46-
$stringValue = preg_replace(
47-
array('/[\r\n]+/', '/\t+/', '/\x0B+/', '/ +/'),
48-
array("\n", "\t", "\x0B", ' '),
49-
$stringValue
50-
);
51-
}
41+
$normalizedString = $stringValue;
42+
43+
$normalizedString = $constraint->ltrim ? ltrim($normalizedString) : $normalizedString;
44+
$normalizedString = $constraint->rtrim ? rtrim($normalizedString) : $normalizedString;
45+
46+
$normalizedString = $constraint->mergeConsecutiveWhitespaces ? preg_replace('/\s+/', ' ', $normalizedString) : $normalizedString;
5247

5348
if (!$invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset)) {
54-
$length = mb_strlen($stringValue, $constraint->charset);
49+
$length = mb_strlen($normalizedString, $constraint->charset);
5550
}
5651

5752
if ($invalidCharset) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ class NotBlank extends Constraint
2828
);
2929

3030
public $message = 'This value should not be blank.';
31+
public $trim;
3132
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public function validate($value, Constraint $constraint)
2929
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\NotBlank');
3030
}
3131

32-
if (false === $value || (empty($value) && '0' != $value)) {
32+
$normalizedValue = $constraint->trim ? trim($value) : $value;
33+
34+
if (false === $normalizedValue || (empty($normalizedValue) && '0' != $normalizedValue)) {
3335
$this->context->buildViolation($constraint->message)
3436
->setParameter('{{ value }}', $this->formatValue($value))
3537
->setCode(NotBlank::IS_BLANK_ERROR)

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ public function getValidValues()
4343
);
4444
}
4545

46+
public function getWhitespaces()
47+
{
48+
return array(
49+
array("\x20"),
50+
array("\x09"),
51+
array("\x0A"),
52+
array("\x0D"),
53+
array("\x0B"),
54+
);
55+
}
56+
4657
public function testNullIsInvalid()
4758
{
4859
$constraint = new NotBlank(array(
@@ -98,4 +109,22 @@ public function testEmptyArrayIsInvalid()
98109
->setCode(NotBlank::IS_BLANK_ERROR)
99110
->assertRaised();
100111
}
112+
113+
/**
114+
* @dataProvider getWhitespaces
115+
*/
116+
public function testInvalidTrimmedValues($value)
117+
{
118+
$constraint = new NotBlank(array(
119+
'message' => 'myMessage',
120+
'trim' => true,
121+
));
122+
123+
$this->validator->validate($value, $constraint);
124+
125+
$this->buildViolation('myMessage')
126+
->setParameter('{{ value }}', '"'.$value.'"')
127+
->setCode(NotBlank::IS_BLANK_ERROR)
128+
->assertRaised();
129+
}
101130
}

0 commit comments

Comments
 (0)
0