ALFA
ALFA

Reputation: 285

BuildForm Symfony 2 - get Selected Value in ChoiceType

How can I get the value of the item selected in my ChoiceType. I want to use it in a other form in my twig. This is my buildForm :

$builder->add('number', ChoiceType::class, array(
            'choices' => array(
                '1'=>'Uns',
                '2'=>'Deux',
                '3'=>'Trois',
            ),
            'required'  => true
        ));

I want to do :

{% if selectedValue == 1 %}
    //do something
{% endif%}

Edit : In my view , I have :

<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
            {{ form_widget(form.number) }}
        </div>

And if I selected a item , I have nothig :

<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
         selected : {{  form.number.vars.value }}
        </div>

Edit 2 : enter image description here

How i can get the selected value of my choiceType

thanks

Upvotes: 2

Views: 1060

Answers (1)

Matteo
Matteo

Reputation: 39380

You can access the form attribute value with vars . As example:

{% if form.number.vars.value == 1 %}
    //do something
{% endif%}

Upvotes: 1

Related Questions