Jelajahi Sumber

[DomCrawler] fixed disabled fields in forms (they are available in the DOM, but their values are not submitted -- whereas before, they were simply removed from the DOM)

Fabien Potencier 14 tahun lalu
induk
melakukan
3380f2a039

+ 6 - 0
src/Symfony/Component/DomCrawler/Field/FormField.php

@@ -23,6 +23,7 @@ abstract class FormField
     protected $value;
     protected $document;
     protected $xpath;
+    protected $disabled;
 
     /**
      * Constructor.
@@ -86,6 +87,11 @@ abstract class FormField
         return true;
     }
 
+    public function isDisabled()
+    {
+        return $this->node->hasAttribute('disabled');
+    }
+
     /**
      * Initializes the form field.
      */

+ 8 - 0
src/Symfony/Component/DomCrawler/Form.php

@@ -83,6 +83,10 @@ class Form extends Link implements \ArrayAccess
     {
         $values = array();
         foreach ($this->fields as $name => $field) {
+            if ($field->isDisabled()) {
+                continue;
+            }
+
             if (!$field instanceof Field\FileFormField && $field->hasValue()) {
                 $values[$name] = $field->getValue();
             }
@@ -106,6 +110,10 @@ class Form extends Link implements \ArrayAccess
 
         $files = array();
         foreach ($this->fields as $name => $field) {
+            if ($field->isDisabled()) {
+                continue;
+            }
+
             if ($field instanceof Field\FileFormField) {
                 $files[$name] = $field->getValue();
             }

+ 8 - 2
tests/Symfony/Tests/Component/DomCrawler/FormTest.php

@@ -73,10 +73,10 @@ class FormTest extends \PHPUnit_Framework_TestCase
                 array(),
             ),
             array(
-                'does not take into account disabled input fields',
+                'takes into account disabled input fields',
                 '<input type="text" name="foo" value="foo" disabled="disabled" />
                  <input type="submit" />',
-                array(),
+                array('foo' => array('Symfony\\Component\\DomCrawler\\Field\\InputFormField', 'foo')),
             ),
             array(
                 'appends the submitted button value',
@@ -201,6 +201,9 @@ class FormTest extends \PHPUnit_Framework_TestCase
 
         $form = $this->createForm('<form><input type="file" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
         $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include file input fields');
+
+        $form = $this->createForm('<form><input type="text" name="foo" value="foo" disabled="disabled" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
+        $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include disabled fields');
     }
 
     public function testSetValues()
@@ -223,6 +226,9 @@ class FormTest extends \PHPUnit_Framework_TestCase
 
         $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
         $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields');
+
+        $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" disabled="disabled" /><input type="submit" /></form>');
+        $this->assertEquals(array(), $form->getFiles(), '->getFiles() does not include disabled file fields');
     }
 
     public function testGetPhpFiles()