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'
This looks to me like a fix for a misbehaviour, rather than a new feature. What about backporting to old versions?
@Massimiliano they can't do that. New features are only allowed on new versions of symfony.
@albert if you see this as something new, yes is a "new feature" but I agree with @Massimiliano , this looks more like a fix.