[DomCrawler] Add support for formaction and formmethod attributes

This commit is contained in:
Christophe Coevoet 2016-11-09 20:43:56 +01:00
parent cd8b9d962b
commit 717cf8a082
2 changed files with 22 additions and 0 deletions

View File

@ -211,6 +211,11 @@ class Form extends Link implements \ArrayAccess
protected function getRawUri()
{
// If the form was created from a button rather than the form node, check for HTML5 action overrides
if ($this->button !== $this->node && $this->button->getAttribute('formaction')) {
return $this->button->getAttribute('formaction');
}
return $this->node->getAttribute('action');
}
@ -227,6 +232,11 @@ class Form extends Link implements \ArrayAccess
return $this->method;
}
// If the form was created from a button rather than the form node, check for HTML5 method override
if ($this->button !== $this->node && $this->button->getAttribute('formmethod')) {
return strtoupper($this->button->getAttribute('formmethod'));
}
return $this->node->getAttribute('method') ? strtoupper($this->node->getAttribute('method')) : 'GET';
}

View File

@ -320,6 +320,12 @@ class FormTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('PATCH', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');
}
public function testGetMethodWithOverride()
{
$form = $this->createForm('<form method="get"><input type="submit" formmethod="post" /></form>');
$this->assertEquals('POST', $form->getMethod(), '->getMethod() returns the method attribute value of the form');
}
public function testGetSetValue()
{
$form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
@ -527,6 +533,12 @@ class FormTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('http://localhost/foo/bar', $form->getUri(), '->getUri() returns path if no action defined');
}
public function testGetUriWithActionOverride()
{
$form = $this->createForm('<form action="/foo"><button type="submit" formaction="/bar" /></form>', null, 'http://localhost/foo/');
$this->assertEquals('http://localhost/bar', $form->getUri(), '->getUri() returns absolute URIs');
}
public function provideGetUriValues()
{
return array(