Skip to content

Loading…

added validation support for optgroups #2331

Merged
merged 2 commits into from

3 participants

@jamescarr

Commits speak for themselves. Changed getOptionsValues() to extract nested values from option groups. This allows for proper validation of Selects that have optgroups in them.

@cgmartin

Looks good :+1:

@weierophinney weierophinney commented on the diff
library/Zend/Form/Element/Select.php
((13 lines not shown))
}
return $values;
}
+
+ private function getOptionValue($key, $optionSpec)
@weierophinney Zend Framework member

protected visibility, please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney commented on the diff
library/Zend/Form/Element/Select.php
@@ -175,9 +175,19 @@ 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)){
@weierophinney Zend Framework member

Add whitespace between if and opening paren, and between closing paren and opening brace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney commented on the diff
library/Zend/Form/Element/Select.php
@@ -175,9 +175,19 @@ 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) {
+ $values[] = $this->getOptionValue($nestedKey, $nestedOptionSpec);
+ }
+ }else{
@weierophinney Zend Framework member

Add whitespace around the else keyword, please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney added a commit that referenced this pull request
@weierophinney weierophinney [#2331] CS fixes
- whitespace
- loop branching
- private -> protected
35a29d2
@weierophinney
Zend Framework member

Incorporated feedback, and merged to release and master branches.

@weierophinney weierophinney merged commit 3cb169c into zendframework:master

1 check passed

Details default The Travis build passed
@ghost Unknown pushed a commit that referenced this pull request
@weierophinney weierophinney [#2331] CS fixes
- whitespace
- loop branching
- private -> protected
6752cb6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 48 additions and 3 deletions.
  1. +12 −2 library/Zend/Form/Element/Select.php
  2. +36 −1 tests/ZendTest/Form/Element/SelectTest.php
View
14 library/Zend/Form/Element/Select.php
@@ -175,9 +175,19 @@ 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)){
@weierophinney Zend Framework member

Add whitespace between if and opening paren, and between closing paren and opening brace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ foreach ($optionSpec['options'] as $nestedKey => $nestedOptionSpec) {
+ $values[] = $this->getOptionValue($nestedKey, $nestedOptionSpec);
+ }
+ }else{
@weierophinney Zend Framework member

Add whitespace around the else keyword, please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $values[] = $this->getOptionValue($key, $optionSpec);
+ }
}
return $values;
}
+
+ private function getOptionValue($key, $optionSpec)
@weierophinney Zend Framework member

protected visibility, please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ return is_array($optionSpec) ? $optionSpec['value'] : $key;
+ }
}
View
37 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());
}
-}
+}
Something went wrong with that request. Please try again.