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

时间:2014-09-16 12:47:17

标签: php forms symfony symfony-forms 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);

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

我尝试了以下代码,

$domdocument = new \DOMDocument;

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

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

$form->set($formfield); 

$form->set($formfield2);

但返回以下错误消息

  

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

1 个答案:

答案 0 :(得分:4)

您正在创建的是:

<__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);