Allow array (with default value) or null in Symfony 2.1 semantic configuration Allow array (with default value) or null in Symfony 2.1 semantic configuration symfony symfony

Allow array (with default value) or null in Symfony 2.1 semantic configuration


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.