Skip to content

Navigation Menu

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 61fd825

Browse filesBrowse files
committed
[OptionsResolver] Support Validation::fromClosure() as allowed value closure
1 parent 22c2f1a commit 61fd825
Copy full SHA for 61fd825

File tree

4 files changed

+29
-4
lines changed
Filter options

4 files changed

+29
-4
lines changed

‎src/Symfony/Component/OptionsResolver/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/OptionsResolver/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3
5+
---
6+
7+
* Convert Validation's `ValidationFailedException` into `false` for allowed values.
8+
49
5.1.0
510
-----
611

‎src/Symfony/Component/OptionsResolver/OptionsResolver.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/OptionsResolver/OptionsResolver.php
+10-4Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\OptionsResolver\Exception\NoSuchOptionException;
1919
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
2020
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
21+
use Symfony\Component\Validator\Exception\ValidationFailedException;
2122

2223
/**
2324
* Validates options and merges them with default values.
@@ -1038,13 +1039,18 @@ public function offsetGet($option, bool $triggerDeprecation = true)
10381039

10391040
foreach ($this->allowedValues[$option] as $allowedValue) {
10401041
if ($allowedValue instanceof \Closure) {
1041-
if ($allowedValue($value)) {
1042+
try {
1043+
if (!$allowedValue($value)) {
1044+
// Don't include closures in the exception message
1045+
continue;
1046+
}
1047+
10421048
$success = true;
10431049
break;
1050+
} catch (ValidationFailedException $e) {
1051+
// Don't include closures in the exception message
1052+
continue;
10441053
}
1045-
1046-
// Don't include closures in the exception message
1047-
continue;
10481054
}
10491055

10501056
if ($value === $allowedValue) {

‎src/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
2525
use Symfony\Component\OptionsResolver\Options;
2626
use Symfony\Component\OptionsResolver\OptionsResolver;
27+
use Symfony\Component\Validator\Validation;
28+
use Symfony\Component\Validator\Constraints\Length;
2729

2830
class OptionsResolverTest extends TestCase
2931
{
@@ -1136,6 +1138,15 @@ function () { return false; },
11361138
$this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
11371139
}
11381140

1141+
public function testResolveFailsIfClosureThrowsValidationException()
1142+
{
1143+
$this->expectException(InvalidOptionsException::class);
1144+
$this->resolver->setDefault('foo', 'bar');
1145+
$this->resolver->setAllowedValues('foo', Validation::createCallable(new Length(['min' => 5])));
1146+
1147+
$this->resolver->resolve();
1148+
}
1149+
11391150
public function testAddAllowedValuesFailsIfUnknownOption()
11401151
{
11411152
$this->expectException(UndefinedOptionsException::class);

‎src/Symfony/Component/OptionsResolver/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/OptionsResolver/composer.json
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"symfony/polyfill-php73": "~1.0",
2222
"symfony/polyfill-php80": "^1.15"
2323
},
24+
"require-dev": {
25+
"symfony/validator": "^5.0"
26+
},
2427
"autoload": {
2528
"psr-4": { "Symfony\\Component\\OptionsResolver\\": "" },
2629
"exclude-from-classmap": [

0 commit comments

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