0

I've been trying to make tree builder configuration with Symfony 3 to parse a configuration like that:

my_bundle:
    import:
        paths:
            - 'some/path'
            - 'another/path'

My TreeBuilder looks like this:

$rootNode
    ->children()
        ->arrayNode('import')
            ->children()
                ->arrayNode('paths')
                    ->addDefaultsIfNotSet()
                    ->defaultValue([])
                    ->cannotBeEmpty()
                ->end()
            ->end()
        ->end()
    ->end();

It is basically like two dimensional array config I would like to get as a result. Could you guys help me figure it out?

Expected parsed config:

['import' => ['paths' => ['some/path', 'another/path']]]
4
  • 1
    And what do you get instead? Commented Sep 21, 2016 at 8:07
  • ->defaultValue() is not applicable to concrete nodes at path "my_bundle.import.paths" Commented Sep 21, 2016 at 8:08
  • And what kind of behaviour do you want to archive? Why do you apply cannotBeEmpty and defaultValue([]) at the same time? [] is empty. Commented Sep 21, 2016 at 8:15
  • cannotBeEmpty is irrelevant at this point. It does even compile as valid configuration tree definition. The thing what I want to achieve is that paths array would be under import key on root array. Commented Sep 21, 2016 at 8:18

1 Answer 1

3

Looks like I forgot to prototype the data structure:

$rootNode
    ->children()
        ->arrayNode('import')
            ->children()
                ->arrayNode('paths')
                    ->prototype('scalar')->end()
                ->end()
            ->end()
        ->end()
    ->end();
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, but it could be empty in this case. That's why I asked why did you put cannotBeEmpty()
How the TreeBuilder would look if no config is set to config.yml? I mean that the default value would be ['import' => ['paths' => []]]?
I believe root node is still required, but import and paths might be empty. But it's just something very easy to be checked once you've got a code

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.