Skip to content

Allow extra options in OptionsResolver #45370

Closed
@kshtompel

Description

@kshtompel

Symfony version(s) affected

master

Description

I have an array of options that has a subset that needs resolving, the rest of it is not important.
There is no possibility to ignore other options only define them and that leads to expose all options for all components that not interested in them or prepare options for each component but this makes no sense to use OptionsResolver after options were prepared.

How to reproduce

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 yields UndefinedOptionsException

Possible Solution

Add possibility to ignore not configured options
and resolve only options that are configured

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

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

$resolver->resolve($options); //should produce
$options = [
    'email' => 'email',
    'phone' => 'phone',
];

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions