Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d1d6e22

Browse files
committed
[Validator] Add string normalization options to the LengthValidator
Add three new options to the LengthValidator: - ltrim - rtrim - mergeConsecutiveWhitespaces
1 parent 009b4d2 commit d1d6e22

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class Length extends Constraint
3939
public $max;
4040
public $min;
4141
public $charset = 'UTF-8';
42+
public $ltrim;
43+
public $rtrim;
44+
public $mergeConsecutiveWhitespaces;
4245

4346
public function __construct($options = null)
4447
{

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ public function validate($value, Constraint $constraint)
3939

4040
$stringValue = (string) $value;
4141

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+
}
52+
4253
if (!$invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset)) {
4354
$length = mb_strlen($stringValue, $constraint->charset);
4455
}

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,39 @@ public function getOneCharset()
9292
);
9393
}
9494

95+
public function getThreeCharactersWithLeadingWhitespaces()
96+
{
97+
return array(
98+
array("\x20123"),
99+
array("\x09üüü"),
100+
array("\x0Aééé"),
101+
array("\x0D123"),
102+
array("\x0Bééé"),
103+
);
104+
}
105+
106+
public function getThreeCharactersWithTrailingWhitespaces()
107+
{
108+
return array(
109+
array("123\x20"),
110+
array("üüü\x09"),
111+
array("ééé\x0A"),
112+
array("123\x0D"),
113+
array("ééé\x0B"),
114+
);
115+
}
116+
117+
public function getThreeCharactersWithConsecutiveWhitespaces()
118+
{
119+
return array(
120+
array("123\x20\x20"),
121+
array("\x09\x09üüü"),
122+
array("é\x0A\x0Aéé"),
123+
array("123\x0D\x0D"),
124+
array("éé\x0B\x0Bé"),
125+
);
126+
}
127+
95128
/**
96129
* @dataProvider getFiveOrMoreCharacters
97130
*/
@@ -125,6 +158,39 @@ public function testValidValuesExact($value)
125158
$this->assertNoViolation();
126159
}
127160

161+
/**
162+
* @dataProvider getThreeCharactersWithLeadingWhitespaces
163+
*/
164+
public function testValidValuesLtrim($value)
165+
{
166+
$constraint = new Length(array('min' => 3, 'max' => 3, 'ltrim' => true));
167+
$this->validator->validate($value, $constraint);
168+
169+
$this->assertNoViolation();
170+
}
171+
172+
/**
173+
* @dataProvider getThreeCharactersWithTrailingWhitespaces
174+
*/
175+
public function testValidValuesRtrim($value)
176+
{
177+
$constraint = new Length(array('min' => 3, 'max' => 3, 'rtrim' => true));
178+
$this->validator->validate($value, $constraint);
179+
180+
$this->assertNoViolation();
181+
}
182+
183+
/**
184+
* @dataProvider getThreeCharactersWithConsecutiveWhitespaces
185+
*/
186+
public function testValidValuesMergeConsecutiveWhitespaces($value)
187+
{
188+
$constraint = new Length(array('min' => 4, 'max' => 4, 'mergeConsecutiveWhitespaces' => true));
189+
$this->validator->validate($value, $constraint);
190+
191+
$this->assertNoViolation();
192+
}
193+
128194
/**
129195
* @dataProvider getThreeOrLessCharacters
130196
*/

0 commit comments

Comments
 (0)