Skip to content
This repository

added validation support for optgroups #2331

Merged
merged 2 commits into from over 1 year ago

3 participants

James Carr Christopher Martin Matthew Weier O'Phinney
James Carr

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.

Christopher Martin

Looks good :+1:

Matthew Weier O'Phinney weierophinney commented on the diff September 13, 2012
library/Zend/Form/Element/Select.php
((13 lines not shown))
180 185
         }
181 186
         return $values;
182 187
     }
  188
+
  189
+    private function getOptionValue($key, $optionSpec)
1
Matthew Weier O'Phinney Owner

protected visibility, please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Matthew Weier O'Phinney weierophinney commented on the diff September 13, 2012
library/Zend/Form/Element/Select.php
@@ -175,9 +175,19 @@ protected function getValueOptionsValues()
175 175
         $values = array();
176 176
         $options = $this->getValueOptions();
177 177
         foreach ($options as $key => $optionSpec) {
178  
-            $value = (is_array($optionSpec)) ? $optionSpec['value'] : $key;
179  
-            $values[] = $value;
  178
+            if(is_array($optionSpec) && array_key_exists('options', $optionSpec)){
1
Matthew Weier O'Phinney Owner

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
Matthew Weier O'Phinney weierophinney commented on the diff September 13, 2012
library/Zend/Form/Element/Select.php
@@ -175,9 +175,19 @@ protected function getValueOptionsValues()
175 175
         $values = array();
176 176
         $options = $this->getValueOptions();
177 177
         foreach ($options as $key => $optionSpec) {
178  
-            $value = (is_array($optionSpec)) ? $optionSpec['value'] : $key;
179  
-            $values[] = $value;
  178
+            if(is_array($optionSpec) && array_key_exists('options', $optionSpec)){
  179
+                foreach ($optionSpec['options'] as $nestedKey => $nestedOptionSpec) {
  180
+                    $values[] = $this->getOptionValue($nestedKey, $nestedOptionSpec);
  181
+                }
  182
+            }else{
1
Matthew Weier O'Phinney Owner

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
Matthew Weier O'Phinney weierophinney referenced this pull request from a commit September 13, 2012
Matthew Weier O'Phinney [#2331] CS fixes
- whitespace
- loop branching
- private -> protected
35a29d2
Matthew Weier O'Phinney

Incorporated feedback, and merged to release and master branches.

Matthew Weier O'Phinney weierophinney merged commit 3cb169c into from September 13, 2012
Matthew Weier O'Phinney weierophinney closed this September 13, 2012
Deleted user Unknown referenced this pull request from a commit September 13, 2012
Matthew Weier O'Phinney [#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.
14  library/Zend/Form/Element/Select.php
@@ -175,9 +175,19 @@ protected function getValueOptionsValues()
175 175
         $values = array();
176 176
         $options = $this->getValueOptions();
177 177
         foreach ($options as $key => $optionSpec) {
178  
-            $value = (is_array($optionSpec)) ? $optionSpec['value'] : $key;
179  
-            $values[] = $value;
  178
+            if(is_array($optionSpec) && array_key_exists('options', $optionSpec)){
  179
+                foreach ($optionSpec['options'] as $nestedKey => $nestedOptionSpec) {
  180
+                    $values[] = $this->getOptionValue($nestedKey, $nestedOptionSpec);
  181
+                }
  182
+            }else{
  183
+              $values[] = $this->getOptionValue($key, $optionSpec);
  184
+            }
180 185
         }
181 186
         return $values;
182 187
     }
  188
+
  189
+    private function getOptionValue($key, $optionSpec)
  190
+    {
  191
+        return is_array($optionSpec) ? $optionSpec['value'] : $key;
  192
+    }
183 193
 }
37  tests/ZendTest/Form/Element/SelectTest.php
@@ -49,6 +49,41 @@ public function testProvidesInputSpecificationForSingleSelect()
49 49
         }
50 50
     }
51 51
 
  52
+    public function testValidateWorksForNestedSelectElementWithSimpleNaming()
  53
+    {
  54
+        $element = new SelectElement();
  55
+        $element->setValueOptions(array(
  56
+          array('label' => 'group 1', 'options' => array(
  57
+            'Option 1' => 'Label 1',
  58
+            'Option 2' => 'Label 2',
  59
+            'Option 3' => 'Label 2',
  60
+          ))));
  61
+
  62
+        $inputSpec = $element->getInputSpecification();
  63
+        $inArrayValidator = $inputSpec['validators'][0];
  64
+
  65
+        $this->assertTrue($inArrayValidator->isValid('Option 1'));
  66
+        $this->assertFalse($inArrayValidator->isValid('Option 5'));
  67
+    }
  68
+
  69
+    public function testValidateWorksForNestedSelectElementWithExplicitNaming()
  70
+    {
  71
+        $element = new SelectElement();
  72
+        $element->setValueOptions(array(
  73
+          array('label' => 'group 1', 'options' => array(
  74
+            array('value' => 'Option 1', 'label'=> 'Label 1'),
  75
+            array('value' => 'Option 2', 'label'=> 'Label 2'),
  76
+            array('value' => 'Option 3', 'label'=> 'Label 3'),
  77
+          ))));
  78
+
  79
+        $inputSpec = $element->getInputSpecification();
  80
+        $inArrayValidator = $inputSpec['validators'][0];
  81
+
  82
+        $this->assertTrue($inArrayValidator->isValid('Option 1'));
  83
+        $this->assertTrue($inArrayValidator->isValid('Option 2'));
  84
+        $this->assertTrue($inArrayValidator->isValid('Option 3'));
  85
+        $this->assertFalse($inArrayValidator->isValid('Option 5'));
  86
+    }
52 87
     public function testProvidesInputSpecificationForMultipleSelect()
53 88
     {
54 89
         $element = new SelectElement();
@@ -137,4 +172,4 @@ public function testDeprecateOptionsInAttributes()
137 172
         ));
138 173
         $this->assertEquals($valueOptions, $element->getValueOptions());
139 174
     }
140  
-}
  175
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.