Reputation: 3948
With Symfony2, I know how to use a Configuration.php file to set up what options are available for a bundle through config.yml. However, I'm not sure how to set one option to default to the value of another one (or if this is easily doable).
What I'm looking for is to have a Configuration something like this:
->scalarNode('option_one')
->defaultValue('peanuts')
->cannotBeEmpty()
->end()
->scalarNode('option_two')
->defaultValue('%option_one%')
->end()
So that with:
# config.yml
my_bundle:
set_one:
option_one: foo
option_two: bar
set_two:
option_one: baz
set_three: ~
For set_one
we would then have option_two
set to bar
.
For set_two
, option_two
would be set to baz
(via the default setting which matches it to option_one
).
And for set_three
, both option_one
and option_two
would be set to the default of peanuts
.
Maybe I'm looking in the wrong place, though... perhaps there is somewhere else in the configuration process that 'missing' values could be caught and set to defaults based on their sibling values (or the wider configuration)?
Upvotes: 2
Views: 396
Reputation: 21249
The docs for validate()
say (emphasis mine):
The expression receives the value of the node and must return it. It can modify it.
So you can leverage this feature to actually set the defaults based on other values as you desire.
->scalarNode('option_one')
->defaultValue('peanuts')
->cannotBeEmpty()
->end()
->scalarNode('option_two')
->defaultNull()
->end()
->validate()
->always(function(array $config) {
if (!$config['option_two']) {
$config['option_two'] = $config['option_one'];
}
return $config;
})
->end()
Upvotes: 2