在提交之前向Symfony表单添加其他字段


Adding Additional Fields to Symfony Form before Submit

我正在使用Goutte,https://github.com/fabpot/goutte,并具有以下代码,

$client = new Client();
$crawler = $client->request('GET', 'Config::get('Eload2::url'));
$form = $crawler->selectButton('Submit')->form();
// add extra fields here
$client->submit($form);

如何在表单提交之前将隐藏的输入字段添加到表单中?

我尝试了以下代码,

$domdocument = new 'DOMDocument;
$formfield = new InputFormField ($domdocument->createElement('__EVENTTARGET', 'ctl00$ContentPlaceHolder1$DDLTelco'));
$formfield2 = new InputFormField ($domdocument->createElement('__EVENTARGUMENT',''));
$form->set($formfield); 
$form->set($formfield2);

但是返回以下错误消息,

InputFormField只能从输入或按钮标记(给定__EVENTTARGET)创建。

您正在创建的是:

<__EVENTTARGET>ctl00$ContentPlaceHolder1$DDLTelco</__EVENTTARGET>
<__EVENTARGUMENT />

当你想要:

<input name="__EVENTTARGET" value="ctl00$ContentPlaceHolder1$DDLTelco" />
<input name="__EVENTARGUMENT" value="" />

试试这个:

$ff = $domdocument->createElement('input');
$ff->setAttribute('name', '__EVENTTARGET');
$ff->setAttribute('value', 'ctl00$ContentPlaceHolder1$DDLTelco');
$formfield = new InputFormField($ff);
$ff = $domdocument->createElement('input');
$ff->setAttribute('name', '__EVENTTARGET');
$ff->setAttribute('value', '');
$formfield2 = new InputFormField($ff);