From bfde7776caf56f38fc7c335696d43d2d5219398f Mon Sep 17 00:00:00 2001 From: James Carr Date: Mon, 10 Sep 2012 10:37:24 -0500 Subject: [PATCH 1/2] added validation support for optgroups --- library/Zend/Form/Element/Select.php | 13 ++++++-- tests/ZendTest/Form/Element/SelectTest.php | 37 +++++++++++++++++++++- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/library/Zend/Form/Element/Select.php b/library/Zend/Form/Element/Select.php index 3700874f14c..912119da6d9 100644 --- a/library/Zend/Form/Element/Select.php +++ b/library/Zend/Form/Element/Select.php @@ -127,7 +127,7 @@ protected function getValidator() if (null === $this->validator) { $validator = new InArrayValidator(array( 'haystack' => $this->getValueOptionsValues(), - 'strict' => false + 'strict' => false, )); $multiple = (isset($this->attributes['multiple'])) @@ -175,8 +175,15 @@ protected function getValueOptionsValues() $values = array(); $options = $this->getValueOptions(); foreach ($options as $key => $optionSpec) { - $value = (is_array($optionSpec)) ? $optionSpec['value'] : $key; - $values[] = $value; + if(is_array($optionSpec) && array_key_exists('options', $optionSpec)){ + foreach ($optionSpec['options'] as $nestedKey => $nestedOptionSpec) { + $value = (is_array($nestedOptionSpec)) ? $nestedOptionSpec['value'] : $nestedKey; + $values[] = $value; + } + }else{ + $value = (is_array($optionSpec)) ? $optionSpec['value'] : $key; + $values[] = $value; + } } return $values; } diff --git a/tests/ZendTest/Form/Element/SelectTest.php b/tests/ZendTest/Form/Element/SelectTest.php index ffb9ef09a96..373231df8d4 100644 --- a/tests/ZendTest/Form/Element/SelectTest.php +++ b/tests/ZendTest/Form/Element/SelectTest.php @@ -49,6 +49,41 @@ public function testProvidesInputSpecificationForSingleSelect() } } + public function testValidateWorksForNestedSelectElementWithSimpleNaming() + { + $element = new SelectElement(); + $element->setValueOptions(array( + array('label' => 'group 1', 'options' => array( + 'Option 1' => 'Label 1', + 'Option 2' => 'Label 2', + 'Option 3' => 'Label 2', + )))); + + $inputSpec = $element->getInputSpecification(); + $inArrayValidator = $inputSpec['validators'][0]; + + $this->assertTrue($inArrayValidator->isValid('Option 1')); + $this->assertFalse($inArrayValidator->isValid('Option 5')); + } + + public function testValidateWorksForNestedSelectElementWithExplicitNaming() + { + $element = new SelectElement(); + $element->setValueOptions(array( + array('label' => 'group 1', 'options' => array( + array('value' => 'Option 1', 'label'=> 'Label 1'), + array('value' => 'Option 2', 'label'=> 'Label 2'), + array('value' => 'Option 3', 'label'=> 'Label 3'), + )))); + + $inputSpec = $element->getInputSpecification(); + $inArrayValidator = $inputSpec['validators'][0]; + + $this->assertTrue($inArrayValidator->isValid('Option 1')); + $this->assertTrue($inArrayValidator->isValid('Option 2')); + $this->assertTrue($inArrayValidator->isValid('Option 3')); + $this->assertFalse($inArrayValidator->isValid('Option 5')); + } public function testProvidesInputSpecificationForMultipleSelect() { $element = new SelectElement(); @@ -137,4 +172,4 @@ public function testDeprecateOptionsInAttributes() )); $this->assertEquals($valueOptions, $element->getValueOptions()); } -} \ No newline at end of file +} From 3cb169c24682711a475624690344d3c40a41c6f5 Mon Sep 17 00:00:00 2001 From: James Carr Date: Mon, 10 Sep 2012 10:49:53 -0500 Subject: [PATCH 2/2] minor refactoring, ran php-cs-fixer on source --- library/Zend/Form/Element/Select.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/library/Zend/Form/Element/Select.php b/library/Zend/Form/Element/Select.php index 912119da6d9..395b4be0eea 100644 --- a/library/Zend/Form/Element/Select.php +++ b/library/Zend/Form/Element/Select.php @@ -127,7 +127,7 @@ protected function getValidator() if (null === $this->validator) { $validator = new InArrayValidator(array( 'haystack' => $this->getValueOptionsValues(), - 'strict' => false, + 'strict' => false )); $multiple = (isset($this->attributes['multiple'])) @@ -177,14 +177,17 @@ protected function getValueOptionsValues() foreach ($options as $key => $optionSpec) { if(is_array($optionSpec) && array_key_exists('options', $optionSpec)){ foreach ($optionSpec['options'] as $nestedKey => $nestedOptionSpec) { - $value = (is_array($nestedOptionSpec)) ? $nestedOptionSpec['value'] : $nestedKey; - $values[] = $value; + $values[] = $this->getOptionValue($nestedKey, $nestedOptionSpec); } }else{ - $value = (is_array($optionSpec)) ? $optionSpec['value'] : $key; - $values[] = $value; + $values[] = $this->getOptionValue($key, $optionSpec); } } return $values; } + + private function getOptionValue($key, $optionSpec) + { + return is_array($optionSpec) ? $optionSpec['value'] : $key; + } }