有一個默認值的形式:Symfony2的形式 - 我怎麼領域覆蓋默認值
class GearType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('options')
->add('model', 'choice', array('choices' => $this->getModelChoices(), 'data' => 2));
}
的要求是形式,可以通過重新賣家預先填寫由URL傳遞參數之一。這也是不錯的功能進行復制的潛在客戶和粘貼鏈接到電子郵件,通訊等
我就是這麼做的:
/**
* @Route("/car/gear")
* @Template()
*/
public function gearAction(Request $request)
{
$form = $this->createForm(new GearType());
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
return 'is valid';
}
} else {
$get = $this->getRequest()->query->all();
if (!empty($get)) {
$normalizer = new GetSetMethodNormalizer();
$form->setData($normalizer->denormalize($get, new Gear())); # look here
}
}
return array('form' => $form->createView());
}
不幸的是場「選項」始終默認值,而不是值傳遞作爲參數。 我試圖改變線#在這裏看看
$gear = $normalizer->denormalize($get, new Gear());
$form = $this->createForm(new GearType(), $gear);
但沒有結果。
看來解決方案正在向GearType對象傳遞附加參數。我不喜歡這個解決方案。有誰知道更好的方法?
遺憾的是沒有 - 它會導致致命錯誤:調用未定義的方法的Symfony \分量\表\表:: setOptions( ) – mrok 2013-02-20 19:13:09
我的歉意,我忘了調用$ form的getData()。上面編輯。 – Lighthart 2013-02-20 21:20:58
$ form-> getData()返回null或我的實體(當我綁定請求並關閉CSRF時) - 它們仍然沒有setOptions方法 – mrok 2013-02-20 22:28:07