I need to define an array node with a given default value in a bundle's semantic configuration. This currently looks like:
$node->arrayNode('foo')
->prototype('scalar')->end()
->defaultValue(array('1', '2', '3'))
->end();
I want to give the user the option to override this array with null
like:
my_bundle:
foo: ~
I cannot use empty arrays ([]
or array()
) instead of null
given that []
should have different semantics from null
.
Is this possible or are there any non-ugly workarounds? Currently I just get an exception:
InvalidTypeException: Invalid type for path "my_bundle.foo". Expected array, but got NULL
You could try something like:
$node->arrayNode('foo')
->beforeNormalization()
->ifTrue(function($v) { return $v === null; })
->then(function($v) { return array(); })
->end()
->prototype('scalar')->end()
->defaultValue(array('1', '2', '3'))
->end();
Or maybe the even simplier:
$node->arrayNode('foo')
->treatNullLike(array())
->prototype('scalar')->end()
->defaultValue(array('1', '2', '3'))
->end();
Otherwise you can use the variableNode rather than the arrayNode; this will give you more freedom but less validation/merging logic out of the box.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With