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 535259e

Browse filesBrowse files
committed
[Validator] Add "trim" option to the NotBlankValidator
1 parent 7378a5f commit 535259e
Copy full SHA for 535259e

File tree

4 files changed

+38
-11
lines changed
Filter options

4 files changed

+38
-11
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/LengthValidator.php
+5-10Lines changed: 5 additions & 10 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+
$normalizedString = $stringValue;
4142

42-
$stringValue = $constraint->ltrim ? ltrim($stringValue) : $stringValue;
43-
$stringValue = $constraint->rtrim ? rtrim($stringValue) : $stringValue;
43+
$normalizedString = $constraint->ltrim ? ltrim($normalizedString) : $normalizedString;
44+
$normalizedString = $constraint->rtrim ? rtrim($normalizedString) : $normalizedString;
4445

45-
if ($constraint->mergeConsecutiveWhitespaces) {
46-
$stringValue = preg_replace(
47-
array('/[\r\n]+/', '/\t+/', '/\x0B+/', '/ +/'),
48-
array("\n", "\t", "\x0B", ' '),
49-
$stringValue
50-
);
51-
}
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

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

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

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