Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a conditional statement when creating a form

I would like to use a conditional statement when creating a form in Symfony.

I am using a choice widget in general case. If the user selects the option "Other", I would like to display an additional text box widget. I suppose this can be done in javascript, but how can I still persist the data from 2 widgets into the same property in my entity?

I have this so far:

  $builder->add('menu', 'choice', array(
        'choices'   => array('Option 1' => 'Option 1', 'Other' => 'Other'),
        'required'  => false,
    ));
  //How to add text box if choice == Other ????

I was planing to use a DataTransfomer, but on 2 widgets??

like image 625
Mick Avatar asked Jul 24 '12 15:07

Mick


1 Answers

I recommend to build a custom type for that, for example ChoiceOrTextType. To this type you add both the choice (named "choice") and the text field (named "text").

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ChoiceOrTextType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('choice', 'choice', array(
                'choices' => $options['choices'] + array('Other' => 'Other'),
                'required' => false,
            ))
            ->add('text', 'text', array(
                'required' => false,
            ))
            ->addModelTransformer(new ValueToChoiceOrTextTransformer($options['choices']))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setRequired(array('choices'));
        $resolver->setAllowedTypes(array('choices' => 'array'));
    }
}

As you already guessed, you also need a data transformer, which can be quite simple:

use Symfony\Component\Form\DataTransformerInterface;

class ValueToChoiceOrTextTransformer implements DataTransformerInterface
{
    private $choices;

    public function __construct(array $choices)
    {
        $this->choices = $choices;
    }

    public function transform($data)
    {
        if (in_array($data, $this->choices, true)) {
            return array('choice' => $data, 'text' => null);
        }

        return array('choice' => 'Other', 'text' => $data);
    }

    public function reverseTransform($data)
    {
        if ('Other' === $data['choice']) {
            return $data['text'];
        }

        return $data['choice'];
    }
}

Now only make the "menu" field a field of that type.

$builder->add('menu', new ChoiceOrTextType(), array(
    'choices'  => array('Option 1' => 'Option 1', 'Option 2' => 'Option 2'),
    'required' => false,
));
like image 53
Bernhard Schussek Avatar answered Nov 04 '22 18:11

Bernhard Schussek