0

I am developing a Symfony2 application and I want to be able to use a configuration file like this:

my_config:
    values: ['val1', 'val2']

So I created the following configuration file:

class Configuration implements ConfigurationInterface {
    public function getConfigTreeBuilder() {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('my_config');

        $rootNode
            ->children()
                ->arrayNode('values')
                    ->prototype('scalar')->end()
                ->end()
            ->end()
        ;

        return $treeBuilder;
    }
}

This configuration, however, lets me add something like:

my_config:
    values: ['val1', 123, false]

Is there a way to enforce array values to be of type String (e.g. something like prototype('string'))?

1 Answer 1

2

There is no string. Check the file https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/Config/Definition/Builder/NodeBuilder.php#L27

Node mapping just expect these values

$this->nodeMapping = array(
    'variable' => __NAMESPACE__.'\\VariableNodeDefinition',
    'scalar' => __NAMESPACE__.'\\ScalarNodeDefinition',
    'boolean' => __NAMESPACE__.'\\BooleanNodeDefinition',
    'integer' => __NAMESPACE__.'\\IntegerNodeDefinition',
    'float' => __NAMESPACE__.'\\FloatNodeDefinition',
    'array' => __NAMESPACE__.'\\ArrayNodeDefinition',
    'enum' => __NAMESPACE__.'\\EnumNodeDefinition',
);

You can check manually in your Conifguration class. Check http://symfony.com/doc/current/cookbook/bundles/configuration.html#processing-the-configs-array this section for further information.

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

Comments

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.