From cb74dbccfcb372497851fb2d7badbc8d5cb91a48 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 19 Jan 2012 11:33:49 +0100 Subject: [PATCH] MINOR Removed overzealous value validation in ListboxField, should silently ignore unknown values (lacking better referential integrity measures in the underlying model layer) --- forms/ListboxField.php | 10 +++------- tests/forms/ListboxFieldTest.php | 12 ------------ 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/forms/ListboxField.php b/forms/ListboxField.php index 43e0b4e37c5..7b835cb0444 100644 --- a/forms/ListboxField.php +++ b/forms/ListboxField.php @@ -162,18 +162,14 @@ function setValue($val) { throw new InvalidArgumentException('No associative arrays allowed multiple=true'); } - if($diff = array_diff($parts, array_keys($this->source))) { - throw new InvalidArgumentException(sprintf( - 'Invalid keys "%s" in value array for multiple=true', - Convert::raw2xml(implode(',', $diff)) - )); - } + // Doesn't check against unknown values in order to allow for less rigid data handling. + // They're silently ignored and overwritten the next time the field is saved. parent::setValue($parts); } else { if(!in_array($val, array_keys($this->source))) { throw new InvalidArgumentException(sprintf( - 'Invalid value "%s" for multiple=true', + 'Invalid value "%s" for multiple=false', Convert::raw2xml($val) )); } diff --git a/tests/forms/ListboxFieldTest.php b/tests/forms/ListboxFieldTest.php index b7384372299..3c9d18705f8 100644 --- a/tests/forms/ListboxFieldTest.php +++ b/tests/forms/ListboxFieldTest.php @@ -90,18 +90,6 @@ function testSetValueFailsOnStringIfChoiceInvalidAndMultipleIsOff() { $field->setValue('invalid'); } - /** - * @expectedException InvalidArgumentException - */ - function testSetValueFailsOnInvalidArrayKeyIfChoiceInvalidAndMultipleIsOn() { - $choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value'); - $field = new ListboxField('Choices', 'Choices', $choices); - $field->multiple = true; - - $obj = new ListboxFieldTest_DataObject(); - $field->setValue(array('a', 'invalid')); - } - function testFieldRenderingMultipleOff() { $choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value'); $field = new ListboxField('Choices', 'Choices', $choices);