New in Symfony 3.3: Added support for formaction and formmethod attributes
December 8, 2016 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
The DomCrawler component eases DOM navigation for HTML and XML documents, making it very useful for functional tests and web scrapers. One of its most popular features allows to fill in and submit forms. But first, you must obtain the object that represents the form via one of its buttons:
1 2 3 4 5 6 7
use Symfony\Component\DomCrawler\Crawler;
$html = '<html> ... </html>';
$crawler = new Crawler($html);
$form = $crawler->selectButton('Save Changes')->form();
// fill in and submit the form...
However, starting from HTML5, buttons of type "submit" can define several attributes to override the original form action, target, method, etc.
1 2 3 4 5 6 7 8
<form action="/save" method="GET">
<!-- ... -->
<input type="submit" value="Save Changes"
formaction="/save-and-close" formmethod="POST">
<input type="submit" value="Save and Add Another"
formaction="/save-and-add" formmethod="POST">
</form>
In Symfony 3.3 we added support for the formaction
and formmethod
attributes. Therefore, you'll always get the right action and method when
getting the form via one of its buttons:
1 2 3 4 5
// ...
$form = $crawler->selectButton('Save Changes')->form();
// $form->getUri() -> '/save-and-close'
$form = $crawler->selectButton('Save and Add Another')->form();
// $form->getUri() -> '/save-and-add'
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.