Symfony2 select option other

108 Views Asked by At

I'm building a form in Symfony2, in a itemType.

Currently, users can type whatever they want in the "typePresentation" field.

I want to let them select 3-4 options (most usual options), and when they select "others", a text field appear and they can type whatever they want.

I think that I can use some jQuery to show a text field when a particular option is selected, but I want to know how to generate 2 fields with the same name, or how to save 1 of 2 fields on the same field of entity.

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('numero',     'hidden')
        ->add('etat', 'choice', array(
                    'choices'   => array('v' => 'Vert', 'o' => 'Orange', 'r' => 'Rouge'),
                    'preferred_choices' => array('v'),
                    'required'  => true,
                    'expanded'  => false,
                    'multiple'  => false))
        ->add('typePresentation', 'text', array('attr' => array('placeholder' => 'Type de la présentation')))
        ->add('datePresentation', 'date', array('empty_data' =>  new \DateTime(),'label' => 'Date de présentation', 'years'=>range(date('Y')-2, date('Y')+5)))
        ->add('iteration', 'textarea', array('attr' => array('placeholder' => "Nom de l'itération")))
        ->add('contenu', 'textarea', array('attr' => array('placeholder' => "Contenu de l'Item")))
        ->add('enregistrer',      'submit');
    ;
}
0

There are 0 best solutions below