Contributed by
Christophe Coevoet
in #20467.
DomCrawler 组件 使HTML和XML文档的导览(navigation)变得容易,使得它在功能测试和网络中抓取非常有用。它的一个最常用的功能是填充和提交表单。但是你必须先通过一个按钮(button)来获取到用于呈现表单的对象:
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... / 填充并提交表单 |
但是,从HTML5开始,button的type “submit”可以定义若干种属性来覆写原始的表单action、target、method等等:
在Symfony 3.3中我们添加了 formaction
和 formmethod
属性的支持。因此,当通过表单的某个button来获取表单(对象)时,你始终可以得到正确的action和method:
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' |