user2560652
user2560652

Reputation:

Symfony2: add form choices after form has been created

I've created a Form Type which specifies the underlying form structure:

namespace ...;

use ...;
use ...;

class TimetableType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('id', 'choice', array(
                'expanded' => true,
                'multiple' => false
            ))
            ->add('schedule', 'submit');
    }

    public function getName()
    {
        return 'timetable';
    }
} 

When I use this type on my controller I want to add choices (multiple entity start-end times) based on a date the user has selected. How do I approach this in symfony2, I can't find anything related in the docs.

Without the Type class I'd have done it this way in my controller:

$form = $this->createFormBuilder(array())
        ->add('id', 'choice', array(
            'choices' => $radioValues,
            'expanded' => true,
            'multiple' => false
        ))
        ->add('schedule', 'submit')
        ->setAction($this->generateUrl('...'))
        ->getForm();

Upvotes: 0

Views: 121

Answers (1)

dmnptr
dmnptr

Reputation: 4304

You need to use form events. It's all right there in the docs:

http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

Upvotes: 0

Related Questions