[Config] Enable cannotBeEmpty along with requiresAtLeastOneElement
This commit is contained in:
parent
954e9f13c5
commit
d40e7e4c2d
@ -428,7 +428,7 @@ class ArrayNodeDefinition extends NodeDefinition implements ParentNodeDefinition
|
||||
$node->setKeyAttribute($this->key, $this->removeKeyItem);
|
||||
}
|
||||
|
||||
if (true === $this->atLeastOne) {
|
||||
if (true === $this->atLeastOne || false === $this->allowEmptyValue) {
|
||||
$node->setMinNumberOfElements(1);
|
||||
}
|
||||
|
||||
@ -490,6 +490,12 @@ class ArrayNodeDefinition extends NodeDefinition implements ParentNodeDefinition
|
||||
);
|
||||
}
|
||||
|
||||
if (false === $this->allowEmptyValue) {
|
||||
throw new InvalidDefinitionException(
|
||||
sprintf('->cannotBeEmpty() is not applicable to concrete nodes at path "%s"', $path)
|
||||
);
|
||||
}
|
||||
|
||||
if (true === $this->atLeastOne) {
|
||||
throw new InvalidDefinitionException(
|
||||
sprintf('->requiresAtLeastOneElement() is not applicable to concrete nodes at path "%s"', $path)
|
||||
|
@ -54,6 +54,7 @@ class ArrayNodeDefinitionTest extends TestCase
|
||||
array('defaultValue', array(array())),
|
||||
array('addDefaultChildrenIfNoneSet', array()),
|
||||
array('requiresAtLeastOneElement', array()),
|
||||
array('cannotBeEmpty', array()),
|
||||
array('useAttributeAsKey', array('foo')),
|
||||
);
|
||||
}
|
||||
@ -285,6 +286,32 @@ class ArrayNodeDefinitionTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function testRequiresAtLeastOneElement()
|
||||
{
|
||||
$node = new ArrayNodeDefinition('root');
|
||||
$node
|
||||
->requiresAtLeastOneElement()
|
||||
->integerPrototype();
|
||||
|
||||
$node->getNode()->finalize(array(1));
|
||||
|
||||
$this->addToAssertionCount(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
|
||||
* @expectedExceptionMessage The path "root" should have at least 1 element(s) defined.
|
||||
*/
|
||||
public function testCannotBeEmpty()
|
||||
{
|
||||
$node = new ArrayNodeDefinition('root');
|
||||
$node
|
||||
->cannotBeEmpty()
|
||||
->integerPrototype();
|
||||
|
||||
$node->getNode()->finalize(array());
|
||||
}
|
||||
|
||||
protected function getField($object, $field)
|
||||
{
|
||||
$reflection = new \ReflectionProperty($object, $field);
|
||||
|
Reference in New Issue
Block a user