Skip to content

Commit 122fae8

Browse files
committed
feature symfony#20467 [DomCrawler] Add support for formaction and formmethod attributes (stof)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DomCrawler] Add support for formaction and formmethod attributes | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a This adds supports for the ``formaction`` and ``formmethod`` of submit elements, which override the values defined on the ``<form>`` element. This works only when you call ``$crawler->form()`` on a Crawler containing a button, not when it contains the ``<form>`` itself of course (as the button override is applied only when using this button to submit, not when using another way). Other button-level overrides are not implemented: - ``formtarget`` is useless as we don't implement ``target`` either (the Crawler does not deal with frame-based pages anyway) - ``formnovalidate`` is ignored, as we don't automatically disable the form validation on ``novalidate`` either, but we require an explicit disabling instead (this might be subject to a separate PR though, as it could make sense) - ``formenctype`` is ignored as we also ignore ``enctype`` (we always submit file fields, even when missing the proper enctype) Commits ------- 717cf8a [DomCrawler] Add support for formaction and formmethod attributes
2 parents 20076b0 + 717cf8a commit 122fae8

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Symfony/Component/DomCrawler/Form.php

+10
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ public function getUri()
211211

212212
protected function getRawUri()
213213
{
214+
// If the form was created from a button rather than the form node, check for HTML5 action overrides
215+
if ($this->button !== $this->node && $this->button->getAttribute('formaction')) {
216+
return $this->button->getAttribute('formaction');
217+
}
218+
214219
return $this->node->getAttribute('action');
215220
}
216221

@@ -227,6 +232,11 @@ public function getMethod()
227232
return $this->method;
228233
}
229234

235+
// If the form was created from a button rather than the form node, check for HTML5 method override
236+
if ($this->button !== $this->node && $this->button->getAttribute('formmethod')) {
237+
return strtoupper($this->button->getAttribute('formmethod'));
238+
}
239+
230240
return $this->node->getAttribute('method') ? strtoupper($this->node->getAttribute('method')) : 'GET';
231241
}
232242

src/Symfony/Component/DomCrawler/Tests/FormTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,12 @@ public function testGetMethod()
320320
$this->assertEquals('PATCH', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');
321321
}
322322

323+
public function testGetMethodWithOverride()
324+
{
325+
$form = $this->createForm('<form method="get"><input type="submit" formmethod="post" /></form>');
326+
$this->assertEquals('POST', $form->getMethod(), '->getMethod() returns the method attribute value of the form');
327+
}
328+
323329
public function testGetSetValue()
324330
{
325331
$form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
@@ -527,6 +533,12 @@ public function testGetUriWithoutAction()
527533
$this->assertEquals('http://localhost/foo/bar', $form->getUri(), '->getUri() returns path if no action defined');
528534
}
529535

536+
public function testGetUriWithActionOverride()
537+
{
538+
$form = $this->createForm('<form action="/foo"><button type="submit" formaction="/bar" /></form>', null, 'http://localhost/foo/');
539+
$this->assertEquals('http://localhost/bar', $form->getUri(), '->getUri() returns absolute URIs');
540+
}
541+
530542
public function provideGetUriValues()
531543
{
532544
return array(

0 commit comments

Comments
 (0)