Also bin ich ziemlich neu bei Symfony und ich versuche, den Controller leicht unterschiedliche Formen basierend auf Controller-Aktionen erstellen zu lassen. Gerade jetzt, ich habe dieseSymfony2: Optionen an buildForm übergeben
//in Controller
public function addLocationEntryAction(Request $request)
{
$entry = new Entry();
$form = $this->get('form.factory')->create(new EntryType('addLocation'), $entry);
return $this->render('OOTNBlogBundle:Blog:addEntry.html.twig', array(
'form' => $form->createView()
));
}
public function addArticleEntryAction(Request $request)
{
$entry = new Entry();
$form = $this->get('form.factory')->create(new EntryType('addArticle'), $entry);
return $this->render('OOTNBlogBundle:Blog:addEntry.html.twig', array(
'form' => $form->createView()
));
}
Und
//in EntryType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', 'text')
->add('continent', 'text', array('required' => false))
->add('country', 'text', array('required' => false))
->add('category', 'text', array('required' => false))
->add('author', 'hidden')
->add('type', 'hidden')
->add('text', 'textarea')
->add('post', 'submit')
;
}
Ich möchte eine Option aus dem Controller in buildForm
passieren, so dass ich etwas tun kann:
public function buildForm(FormBuilderInterface $builder, array $options, $option)
{
$builder
->add('title', 'text')
;
if($option == 'addLocation')
{
$builder
->add('continent', 'text', array('required' => false))
->add('country', 'text', array('required' => false))
;
}
elseif($option == 'addArticle')
{
$builder
->add('category', 'text', array('required' => false))
;
}
$builder
->add('author', 'hidden')
->add('type', 'hidden')
->add('text', 'textarea')
->add('post', 'submit')
;
}
Wie mache ich das? Ich habe das Symfony doc und ähnliche Fragen hier gelesen, aber für meinen Fall scheint nichts passendes zu sein. Ich weiß es nicht.
Works schön, danke – Afunakwa
@ b.b3rn4rd die beste Antwort! Klar, einfach, effektiv. In meinem Fall funktioniert es auch gut –
Beachten Sie, dass 'setDefaultOptions' zugunsten von' configureOptions' veraltet ist. http://symfony.com/blog/new-in-symfony-2-7-form-and-validator-updates#deprecated-setdefaultoptions-in-favor-of-configureoptions – DarksteelPenguin