Symfony 表单:HTML5 数据列表

Symfony Forms: HTML5 datalist

如何实现 HTML5 带有数据库(Doctrine)值的数据列表?

目的:用自动完成的输入替换带有许多选项的选择。

首先,为字段添加新的 FormType:.

<?php
// src/Acme/Form/Type/DatalistType
namespace Acme\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class DatalistType extends AbstractType
{
    public function getParent()
    {
        return TextType::class;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setRequired(['choices']);
    }

    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        $view->vars['choices'] = $options['choices'];
    }

    public function getName()
    {
        return 'datalist';
    }
}

services.yml中:

form.type.datalist_type:
    class: Acme\Form\Type\DatalistType
    tags:
        -  { name: form.type, alias: datalist }

你有表单主题吗?如果是,请跳到下一步,如果不是,请在 app/Resources/views/Form/fields.html.twig 中创建一个新主题并将您的默认 Twig 主题更改为它:

# app/config/config.yml
twig:
    form_themes:
        - ':Form:fields.html.twig'

现在为表单主题中的新字段定义一个模板:

{% block datalist_widget %}
    <input list="{{ id }}_list" {{ block('widget_attributes') }}{% if value is not empty %}value="{{ value }}"{% endif %}>
    <datalist id="{{ id }}_list">
        {% for choice in choices %}
            <option value="{{ choice }}"></option>
        {% endfor %}
    </datalist>
{% endblock %}

FormType 中使用您的字段:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('country', DatalistType::class, ['choices' => ['a', 'b']]); 
}

而不是 ['a', 'b'] 您需要以某种方式从数据库加载您的选择,我建议将它们作为最简单的解决方案传递到表单选项中。

在您的表单中输入:

->add('commerciaux', TextType::class,
            [
                'label'     => 'Apporteur d\'affaire*',
                'attr'      =>
                    [
                        'placeholder'   => 'Apporteur d\'affaire',
                        'list'          => 'bieres'           
                    ]
            ]
        )

在您看来:

                    {{ form_row(formulaire.commerciaux) }}
                    <datalist id="bieres">
                        <option value="Meteor">
                        <option value="Pils">
                        <option value="Kronenbourg">
                        <option value="Grimbergen">
                    </datalist>

我花了一些时间试图解决这个问题,有一个非常简单的解决方案可以解决 Konstantin 的数据库访问问题。它是创建一个以 EntityType 作为其父级的新表单类型。

class DatalistType extends AbstractType
{
    public function getParent() {
        return EntityType::class;
    }
}

然后您可以为此小部件创建一个新模板:

{# views/form/fields.html.twig #}
{% block datalist_widget %}
    <input {{ block('widget_attributes') }} list="{{ form.vars.id }}_list" value="{{ form.vars.value }}" />
    <datalist id="{{ form.vars.id }}_list">
        {% for choice in choices %}
            <option>
                {{ choice.label }}
            </option>
        {% endfor %}
    </datalist>
{% endblock %}

最后,在您正在构建的表单的 buildForm 函数中,您可以使用 DatalistType 和 EntityType 选项添加您的表单元素。

$builder->add('fieldName', DatalistType::class ,
        array('required' => false, 
              'label' => 'fieldLabel', 
              'class' => 'AppBundle\Entity\EntityName',
              'choice_label' => 'entityField'))

在您的表单中输入:

->add('commerciaux', TextType::class,
            [
                'label'     => 'Apporteur d\'affaire*',
                'attr'      =>
                    [
                        'placeholder'   => 'Apporteur d\'affaire',
                        'list'          => 'bieres'           
                    ]
            ]
        )

声明(控制器或视图) 'bieres' => $array_bieres

在您看来:

                    {{ form_row(formulaire.commerciaux) }}
                    <datalist id="bieres">
                       {% for biere in bieres %}
                        <option value="{{ biere }}">
                {% endfor %}
                    </datalist>