Skip to content

Commit 46c5386

Browse files
committed
feature #35400 [RFC][DX][OptionsResolver] Allow setting info message per option (yceruto)
This PR was merged into the 5.1-dev branch. Discussion ---------- [RFC][DX][OptionsResolver] Allow setting info message per option | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | TODO This is a DX proposal that will help in debugging/errors to better understand the meaning of one defined option. This is how you'd use it: ```php $resolver = new OptionsResolver(); $resolver->setDefined('date'); $resolver->setAllowedTypes('date', \DateTime::class); $resolver->setInfo('date', 'A future date'); // <-- NEW // ... ``` This information may be useful for those options where their name cannot be intuitive enough, or their purpose is too complex. Here is an example (based on the example above): ```php // ... $resolver->setAllowedValues('date', static function ($value): bool { return $value >= new \DateTime('now'); }); ``` So, if you introduce a date value that does not match the criteria, you will get this error message: **Before:** ``` The option "date" with value DateTime is invalid. ``` Note that the allowed value is not printable in this case, hence the error message cannot be clear at all. **After:** ``` The option "date" with value DateTime is invalid. Info: A future date. ``` Although a more accurate error message can be triggered within the `\Closure` if desired. Also you'll see this info message on `debug:form` command (see tests), then you have in advance the informative description of any option. What do you think? Commits ------- 0477a06d8a Allow setting one info message per option
2 parents 0d8d7b4 + 824db95 commit 46c5386

9 files changed

+24
-3
lines changed

Console/Descriptor/Descriptor.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,15 @@ protected function collectOptions(ResolvedFormTypeInterface $type)
108108

109109
protected function getOptionDefinition(OptionsResolver $optionsResolver, string $option)
110110
{
111-
$definition = [
111+
$definition = [];
112+
113+
if ($info = $optionsResolver->getInfo($option)) {
114+
$definition = [
115+
'info' => $info,
116+
];
117+
}
118+
119+
$definition += [
112120
'required' => $optionsResolver->isRequired($option),
113121
'deprecated' => $optionsResolver->isDeprecated($option),
114122
];

Console/Descriptor/JsonDescriptor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
7373
}
7474
}
7575
$map += [
76+
'info' => 'info',
7677
'required' => 'required',
7778
'default' => 'default',
7879
'allowed_types' => 'allowedTypes',

Console/Descriptor/TextDescriptor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
114114
];
115115
}
116116
$map += [
117+
'Info' => 'info',
117118
'Required' => 'required',
118119
'Default' => 'default',
119120
'Allowed types' => 'allowedTypes',

Tests/Command/DebugCommandTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ public function testDebugCustomFormTypeOption()
151151
Symfony\Component\Form\Tests\Command\FooType (foo)
152152
==================================================
153153
154+
---------------- -----------%s
155+
Info "Info" %s
154156
---------------- -----------%s
155157
Required true %s
156158
---------------- -----------%s
@@ -209,5 +211,6 @@ public function configureOptions(OptionsResolver $resolver)
209211
$resolver->setNormalizer('foo', function (Options $options, $value) {
210212
return (string) $value;
211213
});
214+
$resolver->setInfo('foo', 'Info');
212215
}
213216
}

Tests/Fixtures/Descriptor/default_option_with_normalizer.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Symfony\Component\Form\Extension\Core\Type\ChoiceType (choice_translation_domain)
33
=================================================================================
44

5+
---------------- -----------%s
6+
Info - %s
57
---------------- -----------%s
68
Required false %s
79
---------------- -----------%s

Tests/Fixtures/Descriptor/deprecated_option.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Symfony\Component\Form\Tests\Console\Descriptor\FooType (bar)
55
Deprecated true
66
--------------------- -----------------------------------
77
Deprecation message "The option "bar" is deprecated."
8+
--------------------- -----------------------------------
9+
Info -
810
--------------------- -----------------------------------
911
Required false
1012
--------------------- -----------------------------------
@@ -15,4 +17,4 @@ Symfony\Component\Form\Tests\Console\Descriptor\FooType (bar)
1517
Allowed values -
1618
--------------------- -----------------------------------
1719
Normalizers -
18-
--------------------- -----------------------------------
20+
--------------------- -----------------------------------

Tests/Fixtures/Descriptor/overridden_option_with_default_closures.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Symfony\Component\Form\Tests\Console\Descriptor\FooType (empty_data)
22
====================================================================
33

44
---------------- ----------------------%s
5+
Info - %s
6+
---------------- ----------------------%s
57
Required false %s
68
---------------- ----------------------%s
79
Default Value: null %s

Tests/Fixtures/Descriptor/required_option_with_allowed_values.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Symfony\Component\Form\Tests\Console\Descriptor\FooType (foo)
33
=============================================================
44

5+
---------------- -----------%s
6+
Info - %s
57
---------------- -----------%s
68
Required true %s
79
---------------- -----------%s

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"symfony/deprecation-contracts": "^2.1",
2121
"symfony/event-dispatcher": "^4.4|^5.0",
2222
"symfony/intl": "^4.4|^5.0",
23-
"symfony/options-resolver": "^5.0",
23+
"symfony/options-resolver": "^5.1",
2424
"symfony/polyfill-ctype": "~1.8",
2525
"symfony/polyfill-mbstring": "~1.0",
2626
"symfony/property-access": "^5.0",

0 commit comments

Comments
 (0)