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 80577f9

Browse filesBrowse files
committed
bug #20122 [Validator] Reset constraint options (ro0NL)
This PR was merged into the 2.7 branch. Discussion ---------- [Validator] Reset constraint options | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20106 | License | MIT | Doc PR | reference to the documentation PR, if any I think it's good to reset the internal pointer of the options array if we depend on `key($options)`. I've added tests to clarify we intentionally check for the first key. The current behavior actually differs for hhvm/php7 - https://3v4l.org/e6r6E Commits ------- 4c6ddd4 reset constraint options
2 parents 8bfb8f2 + 4c6ddd4 commit 80577f9
Copy full SHA for 80577f9

File tree

2 files changed

+34
-0
lines changed
Filter options

2 files changed

+34
-0
lines changed

‎src/Symfony/Component/Validator/Constraint.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraint.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ public function __construct($options = null)
129129
unset($options['value']);
130130
}
131131

132+
if (is_array($options)) {
133+
reset($options);
134+
}
132135
if (is_array($options) && count($options) > 0 && is_string(key($options))) {
133136
foreach ($options as $option => $value) {
134137
if (array_key_exists($option, $knownOptions)) {

‎src/Symfony/Component/Validator/Tests/ConstraintTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/ConstraintTest.php
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,35 @@ public function testGetErrorNameForUnknownCode()
206206
{
207207
Constraint::getErrorName(1);
208208
}
209+
210+
public function testOptionsAsDefaultOption()
211+
{
212+
$constraint = new ConstraintA($options = array('value1'));
213+
214+
$this->assertEquals($options, $constraint->property2);
215+
216+
$constraint = new ConstraintA($options = array('value1', 'property1' => 'value2'));
217+
218+
$this->assertEquals($options, $constraint->property2);
219+
}
220+
221+
/**
222+
* @expectedException \Symfony\Component\Validator\Exception\InvalidOptionsException
223+
* @expectedExceptionMessage The options "0", "5" do not exist
224+
*/
225+
public function testInvalidOptions()
226+
{
227+
new ConstraintA(array('property2' => 'foo', 'bar', 5 => 'baz'));
228+
}
229+
230+
public function testOptionsWithInvalidInternalPointer()
231+
{
232+
$options = array('property1' => 'foo');
233+
next($options);
234+
next($options);
235+
236+
$constraint = new ConstraintA($options);
237+
238+
$this->assertEquals('foo', $constraint->property1);
239+
}
209240
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.