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

sul*_*man 1 php forms symfony-forms symfony domcrawler

我正在使用 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);
Run Code Online (Sandbox Code Playgroud)

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

我尝试了以下代码,

$domdocument = new \DOMDocument;

$formfield = new InputFormField ($domdocument->createElement('__EVENTTARGET', 'ctl00$ContentPlaceHolder1$DDLTelco'));

$formfield2 = new InputFormField ($domdocument->createElement('__EVENTARGUMENT',''));

$form->set($formfield); 

$form->set($formfield2);
Run Code Online (Sandbox Code Playgroud)

但返回以下错误消息,

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

Mar*_*rek 6

您正在创建的是这样的:

<__EVENTTARGET>ctl00$ContentPlaceHolder1$DDLTelco</__EVENTTARGET>
<__EVENTARGUMENT />
Run Code Online (Sandbox Code Playgroud)

当你想要:

<input name="__EVENTTARGET" value="ctl00$ContentPlaceHolder1$DDLTelco" />
<input name="__EVENTARGUMENT" value="" />
Run Code Online (Sandbox Code Playgroud)

尝试这个:

$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);
Run Code Online (Sandbox Code Playgroud)