Skip to content

[OptionsResolver] ContextErrorException on array option with callback allowed type #10202

@codekestrel

Description

@codekestrel

In relation to #8375

I would get the following error when passing an array as an option with a callback allowed type:

ContextErrorException: Notice: Array to string conversion in /vendor/symfony/symfony/src/Symfony/Component/OptionsResolver/OptionsResolver.php line 303

The following alternation resolves the problem:

private function validateOptionValues(array $options)
{
    foreach ($this->allowedValues as $option => $allowedValues) {
        if (isset($options[$option])) {
            if (is_array($allowedValues) && !in_array($options[$option], $allowedValues, true)) {
                throw new InvalidOptionsException(sprintf('The option "%s" has the value "%s", but is expected to be one of "%s"', $option, $options[$option], implode('", "', $allowedValues)));
            }

            if (is_callable($allowedValues) && !call_user_func($allowedValues, $options[$option])) {
                if(! is_scalar($options[$option])) {
                    throw new InvalidOptionsException(sprintf(
                        'The option "%s" of value type "%s" does not have valid content', $option, gettype($options[$option])));
                } else {
                    throw new InvalidOptionsException(sprintf('The option "%s" has the value "%s", which it is not valid', $option, $options[$option]));
                }
            }
        }
    }
}

Is this recognized as a real problem?

This may also solve: #4833

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