1

I have a numeric parameter in the symfony's parameters.yml file and I have a configuration class in my bundle where I validate that the parameter is an integer, but I get the following error in the validation proccess:

[Symfony\Component\Config\Definition\Exception\InvalidTypeException]    
Invalid type for path "page_size". Expected int, but got string. 

Is there a way to specify the parameter type in the parameters.yml file?

EDIT

parameters.yml:

parameters:
    ...
    list_page_size: 15
    ...

config.yml:

...
example:
    page_size: %list_page_size%
...

Configuration.php:

class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritDoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('example');

        $rootNode->children()

            // Some code here
            ->integerNode('page_size')
                ->defaultValue(15)
            ->end()
            // More code here

        ->end();

        return $treeBuilder;
    }
}

EDIT 2

I have discovered that the exception is thrown in the prepend() method in the bundle extension file (DependencyInjection/ExampleExtension.php), but the error is not thrown in the load method of the same file. It seems like if the parameters.yml file was not loaded at prepend method execution time.

2
  • Can you post your config class and your parameters.yml file? Commented Sep 24, 2014 at 12:57
  • If I write page_size: 15 directly in the config.yml file, it runs. Commented Sep 24, 2014 at 15:30

2 Answers 2

2

The exception is simply because the is_int check fails.

A simple solution would be to add a normalization step like:

// Some code here
->integerNode('page_size')
    ->beforeNormalization()
        ->ifString()
        ->then(function ($val) { return strval(intval($val)) === $val ? intval($val) : $val; })
    ->end()
    ->defaultValue(15)
->end()
// More code here

Also take a look at http://symfony.com/doc/current/components/config/definition.html#normalization

Sign up to request clarification or add additional context in comments.

Comments

0

If you entered something like :

page_size: "10"

Then it's a normal behavior. You should not use " :

page_size: 10

1 Comment

That's what I have and it doesn't run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.