Skip to content

Commit

Permalink
bug #10205 [DomCrawler] Fixed incorrect handling of image inputs (rob…
Browse files Browse the repository at this point in the history
…bertkl)

This PR was merged into the 2.3 branch.

Discussion
----------

[DomCrawler] Fixed incorrect handling of image inputs

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #10204
| License       | MIT
| Doc PR        |

A possible approach to fix #10204, but I'm open to suggestions to fix this another way, as this might not be the most 'elegant' way.

Initially, my thoughts were to create a new DomCrawler\Field\FormField subclass, especially for image inputs. However, this does not solve the problem, because such a FormField would still exist under 1 name in the FormFieldRegistry.

Instead, I've changed it to have 2 separate FormFields instead (which both reference the same input node), with different names (.x and .y) so that both values can be set separately and will both be submitted.

Commits
-------

816cf17 [DomCrawler] Fixed incorrect handling of image inputs
  • Loading branch information
fabpot committed Feb 5, 2014
2 parents 6a4d765 + 816cf17 commit e453c45
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Symfony/Component/DomCrawler/Form.php
Expand Up @@ -384,7 +384,24 @@ private function initialize()

// add submitted button if it has a valid name
if ('form' !== $this->button->nodeName && $this->button->hasAttribute('name') && $this->button->getAttribute('name')) {
$this->set(new Field\InputFormField($document->importNode($this->button, true)));
if ('input' == $this->button->nodeName && 'image' == $this->button->getAttribute('type')) {
$name = $this->button->getAttribute('name');
$this->button->setAttribute('value', '0');

// temporarily change the name of the input node for the x coordinate
$this->button->setAttribute('name', $name.'.x');
$this->set(new Field\InputFormField($document->importNode($this->button, true)));

// temporarily change the name of the input node for the y coordinate
$this->button->setAttribute('name', $name.'.y');
$this->set(new Field\InputFormField($document->importNode($this->button, true)));

// restore the original name of the input node
$this->button->setAttribute('name', $name);
}
else {
$this->set(new Field\InputFormField($document->importNode($this->button, true)));
}
}

// find form elements corresponding to the current form
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/FormTest.php
Expand Up @@ -223,6 +223,11 @@ public function provideInitializeValues()
<input type="submit" name="foobar" value="foobar" />',
array('foobar' => array('InputFormField', 'foobar')),
),
array(
'turns an image input into x and y fields',
'<input type="image" name="bar" />',
array('bar.x' => array('InputFormField', '0'), 'bar.y' => array('InputFormField', '0')),
),
array(
'returns textareas',
'<textarea name="foo">foo</textarea>
Expand Down

0 comments on commit e453c45

Please sign in to comment.