Reputation: 183
I'm looking for some help with a custom configuration parameter I'm trying to implement in Symfony 2.1
I'm new but just working with Symfony so far has been great. I'm now trying to make my bundle more user friendly and configurable using config.yml
.
The parameter I'm trying to define is a sequence of default amounts for example in my config I have
mymain:
default_values: [1, 2, 3, 4]
Now with something like that how would you properly use the treebuilder to add the nodes and properly process the configuration?
What I tried was
$rootNode
->children()
->enumNode('default_values')
->values(array(1, 2))
->end()
->end();
With that I am getting the following exception:
Invalid type for path "mymain.defaults". Expected scalar, but got array.
I'd like the configuration to be optional with a default fallback array sequence I specify.
I've also tried the arrayNode but I believe that's for mappings or arrays with key and value pairing which I'm simply trying to configre a sequence of numbers.
Upvotes: 1
Views: 1151
Reputation: 3454
As far as I know the enumNode allows you only to se a single value within a given set of values.
In your example a valid value for default_values
would be 1 or 2 but not an array.
The following configuration allows you to set an array of numeric values with a default in case default_values
is omitted.
$rootNode
->children()
->arrayNode('default_values')
->defaultValue(array(2,3))
->prototype('scalar')
->validate()
->ifTrue(function($v){ return !is_numeric($v); })
->thenInvalid('%s is not a number')
->end()
->end()
->end()
->end();
I hope is what you need.
Upvotes: 2