Skip to content
This repository

[hotfix/ZF-11831] Zend_Form #517

Closed
wants to merge 2 commits into from

2 participants

Adam Lundrigan Matthew Weier O'Phinney
Adam Lundrigan

Form's default translator incorrectly passed to subforms, causing override of subform elements' default translators

Adam Lundrigan [hotfix/ZF-11831] Zend_Form
Form's default translator incorrectly passed to subforms, causing override of subform elements' default translators
2558573
Adam Lundrigan [hotfix/ZF-11831] Use short class names instead of full canonical nam…
…es where appropriate
3eb340a
Matthew Weier O'Phinney

Reviewed, merged, and pushed to master - thanks!b

Matthew Weier O'Phinney weierophinney closed this October 25, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Oct 20, 2011
Adam Lundrigan [hotfix/ZF-11831] Zend_Form
Form's default translator incorrectly passed to subforms, causing override of subform elements' default translators
2558573
Oct 21, 2011
Adam Lundrigan [hotfix/ZF-11831] Use short class names instead of full canonical nam…
…es where appropriate
3eb340a
This page is out of date. Refresh to see the latest.
3  library/Zend/Form/Form.php
@@ -2248,7 +2248,8 @@ public function isValid($data)
2248 2248
             }
2249 2249
         }
2250 2250
         foreach ($this->getSubForms() as $key => $form) {
2251  
-            if (null !== $translator && !$form->hasTranslator()) {
  2251
+            if (null !== $translator && $this->hasTranslator()
  2252
+                    && !$form->hasTranslator()) {
2252 2253
                 $form->setTranslator($translator);
2253 2254
             }
2254 2255
             if (isset($data[$key]) && !$form->isArray()) {
57  tests/Zend/Form/FormTest.php
@@ -4237,4 +4237,61 @@ public function testAddDecoratorsKeepsNonNumericKeyNames()
4237 4237
         $t2 = $this->form->getDecorators();
4238 4238
         $this->assertEquals($t1, $t2);
4239 4239
     }
  4240
+    
  4241
+    /**
  4242
+     * @group ZF-11831
  4243
+     */
  4244
+    public function testElementsOfSubFormReceiveCorrectDefaultTranslator()
  4245
+    {
  4246
+        $isEmptyKey = \Zend\Validator\NotEmpty::IS_EMPTY;
  4247
+        
  4248
+        // Global default translator
  4249
+        $trDefault = new Translator(array(
  4250
+            'adapter' => 'arrayAdapter',
  4251
+            'content' => array(
  4252
+                $isEmptyKey => 'Default'
  4253
+            ),
  4254
+            'locale' => 'en'
  4255
+        ));
  4256
+        Registry::set('Zend_Translate', $trDefault);
  4257
+        
  4258
+        // Translator to use for elements
  4259
+        $trElement = new Translator(array(
  4260
+            'adapter' => 'arrayAdapter',
  4261
+            'content' => array(
  4262
+                $isEmptyKey =>'Element'
  4263
+            ),
  4264
+            'locale' => 'en'
  4265
+        ));
  4266
+        \Zend\Validator\AbstractValidator::setDefaultTranslator($trElement);
  4267
+        
  4268
+        // Change the form's translator
  4269
+        $form = new Form();
  4270
+        $form->addElement(new \Zend\Form\Element\Text('foo', array(
  4271
+            'required'   => true,
  4272
+            'validators' => array('NotEmpty')
  4273
+        )));
  4274
+        
  4275
+        // Create a subform with it's own validator
  4276
+        $sf1 = new SubForm();
  4277
+        $sf1->addElement(new \Zend\Form\Element\Text('foosub', array(
  4278
+            'required'   => true,
  4279
+            'validators' => array('NotEmpty')
  4280
+        )));
  4281
+        $form->addSubForm($sf1, 'Test1');
  4282
+        
  4283
+        $form->isValid(array());
  4284
+
  4285
+        $messages = $form->getMessages();
  4286
+        $this->assertEquals(
  4287
+            'Element', 
  4288
+            @$messages['foo'][$isEmptyKey], 
  4289
+            'Form element received wrong validator'
  4290
+        );
  4291
+        $this->assertEquals(
  4292
+            'Element', 
  4293
+            @$messages['Test1']['foosub'][$isEmptyKey], 
  4294
+            'SubForm element received wrong validator'
  4295
+        );        
  4296
+    }
4240 4297
 }
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.