Formbuilder Symfony2 - 按子类别搜索

时间:2016-06-21 09:54:06

标签: symfony symfony-forms formbuilder

我在我的树枝上试过这个代码:一些愚蠢的测试:/

<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>

我的表单类型中包含此代码 class SearchrobeType扩展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,

            ))
    ;
}

所以,关键是我有两个实体:robeoption和robeoptionvalues,它们有一个toTomany关系,我是Symfony的新手,我想在我的树枝上使用formBuilder,所以它会是这样的:

<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>

我想在标签中显示每个Robeoption,并在每个Robeoption下面显示一个带有输入复选框按钮的Robeoptionsvalue,Any help plz?

1 个答案:

答案 0 :(得分:0)

只有一种方法可以做到这一点:从我的枝条中动态做到这一点

 <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>

然后从控制器

获取选项数组
if ($_request->getMethod() == 'POST') {

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

    }

searchforRobes($ optionsvalues);是Robe存储库中的一个函数