Symfony Options Resolver allow Invalid Options

symfony options resolver github
symfony form options
symfony form empty_data
symfony form validation
symfony 4.3 forms
symfony configuration
symfony form type
symfony components

I am using the optionsResolver component in a silex project to resolve options for configuration. If I don't explicitly set options with setRequired, setOptional, or setDefaults I get an error Fatal error: Uncaught exception 'Symfony\Component\OptionsResolver\Exception\InvalidOptionsException' with message 'The option "option.key" does not exist. Known options are: ...

I want to allow options that are not defined with those methods. I tried to use my own class that extends the class but the class uses to many private methods that would require me to copy/paste most of the class.

Is there a better way to do this?

I use this component in ApiGen a I think you can't add options that aren't specified.

If you know all options, it is the best practice to name them all.

What is your specific use case?

The OptionsResolver Component (Symfony Docs), It allows you to create an options system with required options, defaults, The OptionsResolver component is an improved replacement for the array_replace PHP If you pass an invalid option now, an InvalidOptionsException is thrown:. $resolver-> setDefault ('encryption', null)-> setDefault ('port', null)-> setAllowedTypes ('port', ['null', 'int'])-> setDeprecated ('port', 'acme/package', '1.2', function (Options $options, $value) {if (null === $value) {return 'Passing "null" to option "port" is deprecated, pass an integer instead.';} // deprecation may also depend on another option if ('ssl' === $options ['encryption'] && 456!== $value) {return 'Passing a different port than "456" when the "encryption" option is set to

I solved this by creating two resolvers. One with the fixed option list, the other is where I add options dynamically. I then split the incoming options array into two arrays using array_filter:

$dynamicOptions = array_filter($options, function($k) use ($fixedOptionKeys) {
    if (!in_array($k, $fixedOptionKeys)) {
        return true;
    }
}, ARRAY_FILTER_USE_KEY);

$fixedOptions = array_filter($options, function($k) use ($fixedOptionKeys) {
    if (in_array($k, $fixedOptionKeys)) {
        return true;
    }
}, ARRAY_FILTER_USE_KEY);

The OptionsResolver Component (Symfony 2.3 Docs), The OptionsResolver component helps you configure objects with option arrays. Then, require the vendor/autoload.php file to enable the autoloading receive a Symfony\Component\OptionsResolver\Exception\InvalidOptionsException  The OptionsResolver component is array_replace() on steroids. It allows you to create an options system with required options, defaults, validation (type, value), normalization and more. - symfony/options-resolver

I think this solution will be more pretty and simplier. Just create your own optionsResolver that extends the symfony base one and override the 'resolve' method

Hope it will help

use Symfony\Component\OptionsResolver\OptionsResolver;

class ExtraOptionsResolver extends OptionsResolver
{
    /**
     * Strip options that have been passed to
     * this method to be resolved, and that have not been defined as default or required options
     * The default behaviour is to throw an UndefinedOptionsException
     *
     * @author Seif
     */
    public function resolve(array $options = array())
    {
        // passing by ref in loops is discouraged, we'll make a copy
        $transformedInputOptions = $options;
        foreach ($options as $key => $option) {
            if (!in_array($key, $this->getDefinedOptions())) { // option was not defined
                unset($transformedInputOptions[$key]); // we will eject it from options list
            }
        }

        return parent::resolve($transformedInputOptions);
    }
}

The OptionsResolver Component (Symfony 2.1 Docs), The OptionsResolver Component helps you configure objects with option arrays. It supports For instance, array('null', 'string') allows port to be null or a string . The Symfony PHP framework. Contribute to symfony/symfony development by creating an account on GitHub.

[OptionsResolver] Support simple option validation · Issue #11705 , use Symfony\Component\OptionsResolver\Options; // throws throws InvalidOptionsException if any of the options contain an invalid value validating simple options arrays against a few basic checks (allowed and required  The good, the bad and the options. What I encounter most of the time is the lack of documentation for these options. Moreover, when you have an object-oriented API that needs options, and it returns an object that also needs options, etc., you end up in what I call the “array options hell”. Pros: When you read this, it’s obvious what it does.

symfony/options-resolver: The OptionsResolver component , It allows you to create an options system with required options, defaults, validation (type, value), normalization and more. - symfony/options-resolver. In Symfony 5.1, the OptionsResolver component has introduced an optional fluent interface to configure options and a way to provide extra debug information per option. New in Symfony 5.1: OptionsResolver improvements (Symfony Blog)

Symfony\Component\OptionsResolver\OptionsResolver, class OptionsResolver implements Options Deprecates an option, allowed types or values. bool An option is also considered set if it was set to null. invalid_message_parameters ¶ type: array default: [] When setting the invalid_message option, you may need to include some variables in the string. This can be done by adding placeholders to that option and including the variables in this option:

Comments
  • I am building a silex skeleton. I am using it to require/validate some config options but I need to add additional options that are not specified.
  • In such case I'd use OptionsResolver only for known config options. Something like this (not tested): php $config = array('known' => TRUE, 'unknown' => FALSE); $knonwKeys = array('knonw'); $knonwOptions = array_filter($config, function ($key) { return in_array($key, $knownKeys);, ARRAY_FILTER_USE_KEY); $optionsResolver->resolve($knonwOptions); (Probably not possible to write nice code in comments).