douhuan1950 2015-07-09 18:44
浏览 62

Silex Form Choice字段

I'm using Symfony forms and Twig to create a filter but something weird is going on and I can't figure out why. I have 4 choice fields, the first 2 choice fields are returning the correct values but the last 2 choice fields are returning the same values as the second choice field.

this is the code for the Controller

// Create Form
    $filterForm = $app['form.factory']->createNamed('filterForm')

        // search
        ->add('title', 'text')

        // date
        ->add('dates', 'choice', array(
            'choices' => $DatesForCombo,
            'placeholder' => 'Kies...',
            'required' => false))

        // category
        ->add('categories', 'choice', array(
            'choices' => $CategoriesForCombo,
            'placeholder' => 'Kies...',
            'required' => false))

        // location
        ->add('locations', 'choice', array(
            'choices' => $LocationsForCombo,
            'placeholder' => 'Kies...', 
            'required' => false))

        // organiser
        ->add('organisers', 'choice', array(
            'choices' => $OrganisersForCombo,
            'placeholder' => 'Kies...',
            'required' => false))

        // is_gentian
        ->add('gents',null,array(
            'label' => 'Enkel Gents gesproken',
            'required' => false))

        ->add('gentsGesproken', 'checkbox', array(
            'value' => 'Y',
            'required' => false))

        // is_free
        ->add('gratis',null,array(
            'label' => 'Enkel gratis events',
            'required' => false))

        ->add('gratisEvents', 'checkbox', array(
            'value' => 'Y',
            'required' => false));

This is the code for the Twig Template

<form action="{{ path('events.index') }}" method="post"  {{ form_enctype(filterForm) }}  novalidate="novalidate" role="form">
            <fieldset>
                <legend>Filter Events</legend>
                <div class="form-group">
                    {{ form_widget(filterForm.title, { 'attr' : { 'class': 'form-control', 'name' : 'filterform[title]', 'id':'filterform_title' }} ) }}
                </div>

                <!-- filter dates -->
                <div class="form-group">
                    {{ form_widget(filterForm.dates, { 'attr' : { 'class': 'form-control', 'name' : 'filterform[day]', 'id':'filterform_title' }} ) }} 
                </div>

                <!-- filter categories -->
                <div class="form-group">
                    {{ form_widget(filterForm.categories, { 'attr' : { 'class': 'form-control', 'name' : 'filterform[category]', 'id':'filterform_categories' }} ) }}
                </div>

                <!-- filter location -->
                <div class="form-group">
                    {{ form_widget(filterForm.locations, { 'attr' : { 'class': 'form-control', 'name' : 'filterform[location]', 'id':'locations' }} ) }}
                </div>

                <!-- filter organisers -->
                <div class="form-group">
                    {{ form_widget(filterForm.organisers, { 'attr' : { 'class': 'form-control', 'name' : 'filterform[organiser]', 'id':'organisers' }} ) }}
                </div>

                <div class="form-group">
                    <!-- filter chckbx Gents gesproken -->
                    <label>
                        {{ form_widget(filterForm.gentsGesproken) }}                
                        {{ form_label(filterForm.gents) }}
                    </label>
                    <!-- filter chckbx Gratis events -->
                    <label>
                        {{ form_widget(filterForm.gratisEvents) }}                 
                        {{ form_label(filterForm.gratis) }}
                    </label>
                </div>

                <input type="hidden" id="filterform__token" name="filterform[_token]" value="lJ3pQTYX8yEbYpDhmHH6V_ktwtbz_5BdWP0Fss6Z7s0" />
                <button type="submit" id="filterform_filter" name="filterform[filter]" class="btn btn-primary pull-right">Filter</button>
            </fieldset>
        </form>

The 4 arrays used are of course different, can someone please help me?

  • 写回答

1条回答 默认 最新

  • douchenchepan6465 2015-07-21 19:33
    关注

    I found the solution, some of the html characters where not encoded correctly, even though my database was in utf8mb5_general_ci and I use UTF-8 in my templates, the characters where shown as a �. Looks like Silex Forms can't handle a choice field with a character that isn't encoded. Never thought that those 2 problems where connected but it was just a matter of using the utf8_encode() method to fill the arrays used in the choice fields. In my twig templates I used

    // create an array for the organiser combobox...
    $OrganisersForCombo = array();
    $numberOfOrganisers = count($AllOrganisers);
    for($i = 0 ; $i <= $numberOfOrganisers-1 ; $i++){
        $title = $AllOrganisers[$i]['title'];
        $OrganisersForCombo[$i+1] = utf8_encode($title);
    }
    

    In my Twig templates I used

    {{ data|convert_encoding('UTF-8', 'ASCII') }}
    

    Like so: http://twig.sensiolabs.org/doc/filters/convert_encoding.html

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算