2011-12-14 71 views
1

我需要爲我的表單設置默認值。有代碼,從我的控制器:Symfony 2.我如何設置實體的嵌入表單的默認值?

$form = $this->createFormBuilder()->add('user', new Form\UserType($user)) 
            ->add('client', new Form\ClientType($client)) 
            ->getForm(); 

而且我有兩個entites的:用戶&客戶端。那麼,我如何從entites設置默認值?

回答

-2

在每個用戶的表單類和客戶端類,你可以設置默認值是這樣的:

public function configure() { 
    $this->setDefault('yourfield', $defaultvalue); 
} 
+0

不能在Symfony2上工作。這似乎與Symfony 1.x有關。 – 2012-01-03 13:37:05

2

我設置的默認值,我的文本字段這樣

->add('firstname', 'text', array('attr' => array('value' => 'bla'))) 

爲一個實體,您可以將空值設置爲false並填充prefrred_choices陣列

->add('language', 'entity', array('empty_value' => false, 'preferred_choices' => array('2'), 'class' => 'CPAppUserBundle:Language',))