Search code examples
symfonysymfony-formsformbuilder

Formbuilder Symfony2 - Searching by subcategory


I have tried this code in my twig : some stupid tests :/

<form class="form-horizontal" id="form-search" action="{{path('search_robe')}}" method="POST" enctype="multipart/form-data">

     {% for robeoption in robeoptions %}
                {% if robeoption.values|length>0 %}
                    <div class="col-md-3 col-sm-3 col-xs-3 ">
                        <div class="Robeoption-title">
                            {{robeoption.translate.title}} 
                        </div>
                        <div class="Robeoption-value">
                            {% for robeoptionval in robeoption.values %}
                                <label>
        <input type="checkbox" class="radio" value="1" name="{{robeoption.translate.title}}" />{{robeoptionval.translate.title}}</label>

                            {% endfor %}
                        </div>
                    </div>
                {% endif %}
            {% endfor %}
</form>

I have this code in my Form Type class SearchrobeType extends AbstractType {

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('robeoptions', 'entity', array(
                'required' => false,
                'class' => 'BrainAdminBundle:RobeOption',
                /** @Ignore */
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('s')
                    ->Join('s.values', 'values')
                    ->groupBy('s.id');
                },
                'label' => true,
                'multiple' => true,
                'expanded' => true,

            ))
            ->add('robeoptionvalue', 'entity', array(
                'required' => false,
                'class' => 'BrainAdminBundle:RobeOptionValue',
                /** @Ignore */
                'label' => false,
                'multiple' => true,
                'expanded' => true,

            ))
    ;
}

So, the point is that i Have Two entities : robeoption and robeoptionvalues which have a oneTomany relation , I'm new to Symfony and I want to use the formBuilder in my twig so it will be something like that instead :

<form class="form-horizontal" id="form-search" action="{{path('search_robe')}}" method="POST" enctype="multipart/form-data">

   {{form_widget(form.robeoptions)}}  //
        {{form_widget(form.robeoptionvalue)}}

</form>

I want to show each Robeoption in a label and just under each one its Robeoptionsvalue with an input checkbox button, Any help plz?


Solution

  • There is only one way to do that : it's to do it dynamically from my twig `

     <form class="form-horizontal col-md-12 col-sm-12 col-xs-12" id="form-search" action="{{path('searchrobe')}}" method="POST" enctype="multipart/form-data">
    
        <div class="sh" style="display:none;"> 
        {{form_row(form._token)}}
        <div class="col-md-12 col-sm-6 search-slide">
    
            {% for robeoption in robeoptions %}
                {% if robeoption.values|length>0 %}
                    <div class="col-md-4 col-sm-4 col-xs-4 ">
                        <div class="Robeoption-title">
                            <label> {{robeoption.translate.title}} </label>
                        </div>
                        <div class="Robeoption-value">
                            {% for robeoptionval in robeoption.values %}
    
                                    <input type="checkbox" name="robeoptionValues[{{robeoptionval.id}}]" />{{robeoptionval.translate.title}}<br>
    
                            {% endfor %}
                        </div>
                    </div>
    
                {% endif %}
    
            {% endfor %}
    
        </div>
    
        <div class="col-md-12 col-sm-12 search-slide">
            <div class="col-md-12 col-sm-12 contour">
                <button class="btn btn-default btn-slider">{{'search'|trans({})}}</button>
            </div>
        </div>
    </div>
    </form>
    

    And then get the array of options from controller

    if ($_request->getMethod() == 'POST') {
    
            $optionsvalues=$_request->request->get('robeoptionValues');
            $robe = $_em->getRepository('BrainAdminBundle:Robe')->searchforRobes($optionsvalues);         
    
        }
    

    searchforRobes($optionsvalues); Is a function in the Robe Repository