Skip to content

Commit

Permalink
[DomCrawler] fixed disabled fields in forms (they are available in th…
Browse files Browse the repository at this point in the history
…e DOM, but their values are not submitted -- whereas before, they were simply removed from the DOM)
  • Loading branch information
fabpot committed Aug 23, 2011
1 parent 04a549b commit 3380f2a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/Symfony/Component/DomCrawler/Field/FormField.php
Expand Up @@ -23,6 +23,7 @@ abstract class FormField
protected $value;
protected $document;
protected $xpath;
protected $disabled;

/**
* Constructor.
Expand Down Expand Up @@ -86,6 +87,11 @@ public function hasValue()
return true;
}

public function isDisabled()
{
return $this->node->hasAttribute('disabled');
}

/**
* Initializes the form field.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/DomCrawler/Form.php
Expand Up @@ -83,6 +83,10 @@ public function getValues()
{
$values = array();
foreach ($this->fields as $name => $field) {
if ($field->isDisabled()) {
continue;
}

if (!$field instanceof Field\FileFormField && $field->hasValue()) {
$values[$name] = $field->getValue();
}
Expand All @@ -106,6 +110,10 @@ public function getFiles()

$files = array();
foreach ($this->fields as $name => $field) {
if ($field->isDisabled()) {
continue;
}

if ($field instanceof Field\FileFormField) {
$files[$name] = $field->getValue();
}
Expand Down
10 changes: 8 additions & 2 deletions tests/Symfony/Tests/Component/DomCrawler/FormTest.php
Expand Up @@ -73,10 +73,10 @@ public function provideInitializeValues()
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',
Expand Down Expand Up @@ -201,6 +201,9 @@ public function testGetValues()

$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()
Expand All @@ -223,6 +226,9 @@ public function testGetFiles()

$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()
Expand Down

0 comments on commit 3380f2a

Please sign in to comment.