[Form] raise exception when label for choice cannot be read
This commit is contained in:
parent
a3cbf6bbf6
commit
4a5dadb520
@ -254,12 +254,17 @@ class ChoiceList implements ChoiceListInterface
|
|||||||
* @param array $labels The labels corresponding to the choices.
|
* @param array $labels The labels corresponding to the choices.
|
||||||
* @param array $preferredChoices The preferred choices.
|
* @param array $preferredChoices The preferred choices.
|
||||||
*
|
*
|
||||||
|
* @throws \InvalidArgumentException If the structures of the choices and labels array do not match.
|
||||||
* @throws InvalidConfigurationException If no valid value or index could be created for a choice.
|
* @throws InvalidConfigurationException If no valid value or index could be created for a choice.
|
||||||
*/
|
*/
|
||||||
protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, array $labels, array $preferredChoices)
|
protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, array $labels, array $preferredChoices)
|
||||||
{
|
{
|
||||||
// Add choices to the nested buckets
|
// Add choices to the nested buckets
|
||||||
foreach ($choices as $group => $choice) {
|
foreach ($choices as $group => $choice) {
|
||||||
|
if (!isset($labels[$group])) {
|
||||||
|
throw new \InvalidArgumentException('The structures of the choices and labels array do not match.');
|
||||||
|
}
|
||||||
|
|
||||||
if (is_array($choice)) {
|
if (is_array($choice)) {
|
||||||
// Don't do the work if the array is empty
|
// Don't do the work if the array is empty
|
||||||
if (count($choice) > 0) {
|
if (count($choice) > 0) {
|
||||||
|
@ -173,4 +173,15 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
|
|||||||
$choices = array($this->obj2, $this->obj3, 'foobar');
|
$choices = array($this->obj2, $this->obj3, 'foobar');
|
||||||
$this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices));
|
$this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function testNonMatchingLabels()
|
||||||
|
{
|
||||||
|
$this->list = new ChoiceList(
|
||||||
|
array($this->obj1, $this->obj2),
|
||||||
|
array('A')
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user