Skip to content

[DomCrawler] Add support for formaction and formmethod attributes #20467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Symfony/Component/DomCrawler/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ public function getUri()

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');
}

Expand All @@ -227,6 +232,11 @@ public function getMethod()
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';
}

Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ public function testGetMethod()
$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>');
Expand Down Expand Up @@ -527,6 +533,12 @@ public function testGetUriWithoutAction()
$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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you are testing getUri() but in your PR you have changed getRawUri(), right?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe because getRawUri is a protected method (can't be tested directly), which is called by public getUri method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed. getUri (defined in the parent class) calls getRawUri and then resolve the URI to account for non-absolute ones (like in this test)

}

public function provideGetUriValues()
{
return array(
Expand Down