8000 [Validator] Add "ltrim" and "rtrim" options to the IpValidator · symfony/symfony@ee3cc2f · GitHub
[go: up one dir, main page]

Skip to content

Commit ee3cc2f

Browse files
committed
[Validator] Add "ltrim" and "rtrim" options to the IpValidator
1 parent 1e1218b commit ee3cc2f

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ class Ip extends Constraint
7272

7373
public $message = 'This is not a valid IP address.';
7474

75+
public $ltrim;
76+
public $rtrim;
77+
7578
/**
7679
* {@inheritdoc}
7780
*/

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public function validate($value, Constraint $constraint)
4242

4343
$value = (string) $value;
4444

45+
$value = $constraint->ltrim ? ltrim($value) : $value;
46+
$value = $constraint->rtrim ? rtrim($value) : $value;
47+
4548
switch ($constraint->version) {
4649
case Ip::V4:
4750
$flag = FILTER_FLAG_IPV4;

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,56 @@ public function getValidIpsV4()
8080
);
8181
}
8282

83+
/**
84+
* @dataProvider getValidIpsV4WithLeadingWhitespaces
85+
*/
86+
public function testValidIpsV4WithLeadingWhitespaces($ip)
87+
{
88+
$this->validator->validate($ip, new Ip(array(
89+
'version' => Ip::V4,
90+
'ltrim' => true,
91+
)));
92+
93+
$this->assertNoViolation();
94+
}
95+
96+
public function getValidIpsV4WithLeadingWhitespaces()
97+
{
98+
return array(
99+
array("\x200.0.0.0"),
100+
array("\x09\x0910.0.0.0"),
101+
array("\x0A123.45.67.178"),
102+
array("\x0D\x0D172.16.0.0"),
103+
array("\x00192.168.1.0"),
104+
array("\x0B\x0B224.0.0.1"),
105+
);
106+
}
107+
108+
/**
109+
* @dataProvider getValidIpsV4WithTrailingWhitespaces
110+
*/
111+
public function testValidIpsV4WithTrailingWhitespaces($ip)
112+
{
113+
$this->validator->validate($ip, new Ip(array(
114+
'version' => Ip::V4,
115+
'rtrim' => true,
116+
)));
117+
118+
$this->assertNoViolation();
119+
}
120+
121+
public function getValidIpsV4WithTrailingWhitespaces()
122+
{
123+
return array(
124+
array("0.0.0.0\x20"),
125+
array("10.0.0.0\x09\x09"),
126+
array("123.45.67.178\x0A"),
127+
array("172.16.0.0\x0D\x0D"),
128+
array("192.168.1.0\x00"),
129+
array("224.0.0.1\x0B\x0B"),
130+
);
131+
}
132+
83133
/**
84134
* @dataProvider getValidIpsV6
85135
*/

0 commit comments

Comments
 (0)
0