【问题标题】:Silex Form Choice fieldSilex 表单选择字段
【发布时间】:2015-07-09 18:44:54
【问题描述】:

我正在使用 Symfony 表单和 Twig 创建过滤器,但发生了一些奇怪的事情,我不知道为什么。我有 4 个选择字段,前 2 个选择字段返回正确的值,但最后 2 个选择字段返回与第二个选择字段相同的值。

这是控制器的代码

// 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));

这是 Twig 模板的代码

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

使用的4个数组当然不同,谁能帮帮我?

【问题讨论】:

标签: php forms symfony twig silex


【解决方案1】:

我找到了解决方案,尽管我的数据库在 utf8mb5_general_ci 中并且我在模板中使用了 UTF-8,但某些 html 字符的编码不正确,这些字符显示为 �。看起来 Silex Forms 无法处理带有未编码字符的选择字段。从来没有想过这两个问题是相互关联的,但这只是使用 utf8_encode() 方法来填充选择字段中使用的数组的问题。在我的树枝模板中,我使用了

// 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);
}

在我使用的 Twig 模板中

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

像这样:http://twig.sensiolabs.org/doc/filters/convert_encoding.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 2021-09-29
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    相关资源
    最近更新 更多