8000 [OptionsResolver] Is it possible to allow/ignore extra options? · Issue #22536 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[OptionsResolver] Is it possible to allow/ignore extra options? #22536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
skafandri opened this issue Apr 26, 2017 · 5 comments
Closed

[OptionsResolver] Is it possible to allow/ignore extra options? #22536

skafandri opened this issue Apr 26, 2017 · 5 comments

Comments

@skafandri
Copy link
Contributor
Q A
Bug report? no
Feature request? no
BC Break report? no
RFC? yes
Symfony version master

I have an array of options that has a subset that needs resolving, the rest of it is not important. Is it possible to ignore the rest of the array?
A similar behavior to Form allow_extra_fields

Example: email and phone are required, everything else just doesn't matter.

$options = [
    'email' => 'email',
    'phone' => 'phone',
    //many other keys
];

$resolver = new \Symfony\Component\OptionsResolver\OptionsResolver();
$resolver->setRequired(['email', 'phone']);
$resolver->resolve($options);

This obviously yields UndefinedOptionsException

@ro0NL
Copy link
Contributor
ro0NL commented Apr 26, 2017

Yes :) $resolver->setDefined(array_keys($options))

#7979

@skafandri
Copy link
Contributor Author

so simple it made me laugh :))

very PHPish :)

@rvanlaak
Copy link
Contributor

Came here via Google. Adding them to setDefined actually results in the the values being returned.

Our use case instead is to allow extra options without the UndefinedOptionsException gets thrown, but the options should not get resolved / outputted.

@kshtompel
Copy link
kshtompel commented Feb 9, 2022

Agree with @rvanlaak
there should possibility to define options you are interested in to be resolved and to skip other

With this answer I define all options in all places where set of options are used or prepare them each time before I use them but it makes no sense to use OptionsResolver after I'd already prepared them

@ephrin
Copy link
ephrin commented Dec 12, 2023

For those who googled this page. \Symfony\Component\OptionsResolver\OptionsResolver::setIgnoreUndefined() can be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants
0