如何为 html5 数据列表定义自定义表单类型类以处理 Symfony 中的 entityType >3.4 [英] How to define a custom form type class for html5 datalist in order to handle entityType in Symfony > 3.4

查看:16
本文介绍了如何为 html5 数据列表定义自定义表单类型类以处理 Symfony 中的 entityType >3.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 datalist 设置自定义表单类型,并且使用预设选项可以正常工作,但我无法设置它以使其处理 EntityType.

这是我的工作代码

<?php//路径和文件名///src/form/type/DatalistType.php命名空间 App\Form\Type;使用 Symfony\Component\Form\AbstractType;使用 Symfony\Component\Form\FormBuilderInterface;使用 Symfony\Component\Form\FormInterface;使用 Symfony\Component\OptionsResolver\OptionsResolver;使用 Symfony\Component\Form\FormView;使用 Symfony\Component\Form\Extension\Core\Type\ChoiceType;使用 Doctrine\ORM\EntityManagerInterface;使用 Symfony\Bridge\Doctrine\Form\Type\EntityType;//暂时未使用类 DatalistType 扩展 AbstractType {私人 $entityManager;公共函数 __construct(EntityManagerInterface $entityManager) {$this->entityManager = $entityManager;}公共函数 configureOptions(OptionsResolver $resolver) {$resolver->setDefaults(['选择' =>['数学' =>'数学','物理' =>'物理','化学' =>'化学',],]);}公共函数 getParent() {返回选择类型::类;}公共函数 setDefaultOptions(OptionsResolverInterface $resolver) {$resolver->setRequired(['choices']);}公共函数 buildView(FormView $view, FormInterface $form, array $options) {$view->vars['choices'] = $options['choices'];}公共函数 getName() {返回数据列表";}}

<?php//路径和文件名///src/form/DegreeType.php命名空间 App\Form;使用 App\Entity\Degree;使用 Symfony\Component\Form\AbstractType;使用 Symfony\Component\Form\FormBuilderInterface;使用 Symfony\Component\OptionsResolver\OptionsResolver;使用 Symfony\Component\Form\Extension\Core\Type\ChoiceType;使用 App\Form\Type\DatalistType;class degreeType 扩展 AbstractType {公共函数 buildForm(FormBuilderInterface $builder, array $options) {$builder->add('degree', DatalistType::class, ['占位符' =>'选择硕士学位',]);}公共函数 configureOptions(OptionsResolver $resolver) {$resolver->setDefaults(['data_class' =>学位::班级,]);}}

<预><代码>//树枝模板//路径和文件名//模板/表单/fields.html.twig?>{% 阻止 datalist_widget %}<div class="form-group"><input list="{{ id }}_list" {{ block('widget_attributes') }} class="form-control"><datalist id="{{ id }}_list">{% 用于选择中的选择 %}<option value="{{choice }}"></option>{% 结束为 %}</数据列表>

{% 结束块 %}

<预><代码>//config/packages/twig.yaml枝条:路径:['%kernel.project_dir%/templates']调试: '%kernel.debug%'strict_variables: '%kernel.debug%'form_themes: ['form/fields.html.twig']

我更改了 getParent() 方法以返回一个 EntityType::class

public function getParent() {返回实体类型::类;}

在 configureOptions() 方法中删除了 $resolver 的默认值

public function configureOptions(OptionsResolver $resolver) {}

然后在表单构建器中

->add('degree',DatalistType::class , ['标签' =>'选择硕士学位','类' =>学位::班级])

我希望它适用于静态值,但它没有.

我在这里读过任何类型的问题

Symfony 表单:HTML5 数据列表

但我认为发布的答案不完整,或者是针对旧版本的 Symfony,而不是针对 > 3.4

解决方案

解决方案是去掉DatalistType里面的所有方法只留下构造函数和 getParent(): EntityType::class

<?php//路径和文件名///src/form/type/DatalistType.php命名空间 App\Form\Type;使用 Symfony\Component\Form\AbstractType;使用 Doctrine\ORM\EntityManagerInterface;使用 Symfony\Bridge\Doctrine\Form\Type\EntityType;类 DatalistType 扩展 AbstractType {私人 $entityManager;公共函数 __construct(EntityManagerInterface $entityManager) {$this->entityManager = $entityManager;}公共函数 getParent() {返回实体类型::类;}}

然后,更改模板

<预><代码>{% 阻止 datalist_widget %}<div class="form-group"><input {{ block('widget_attributes') }} list="{{ form.vars.id }}_list" value="{{ form.vars.value }}" class="form-control" ><datalist id="{{ form.vars.id }}_list">{% 用于选择中的选择 %}<选项>{{选择.标签}}</选项>{% 结束为 %}</数据列表>

{% 结束块 %}

效果很好!!!

I'm setting up a custom form type for datalist and it works fine using a preset choices, but I'm unable to set up it in order to let it handle an EntityType.

That's my working code

<?php

// path and filename
// /src/form/type/DatalistType.php

namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;  // unused at the moment

class DatalistType extends AbstractType {

    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager) {
        $this->entityManager = $entityManager;
    }

    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults([
            'choices' => [
                    'Math' => 'Math',
                    'Physics' => 'Physics',
                    'Chemistry' => 'Chemistry',
                ],
        ]);
    }    

    public function getParent() {
        return ChoiceType::class;
    }

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

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

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

<?php

// path and filename
// /src/form/DegreeType.php

namespace App\Form;

use App\Entity\Degree;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use App\Form\Type\DatalistType;


class DegreeType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder

            ->add('degree', DatalistType::class, [
                'placeholder' => 'Choose a master degree',
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults([
            'data_class' => Degree::class,
        ]);
    }
}


// TWIG TEMPLATE
// path and filename
// templates/form/fields.html.twig
?>

{% block datalist_widget %}
        <div class="form-group">
            <input list="{{ id }}_list" {{ block('widget_attributes') }} class="form-control">
            <datalist id="{{ id }}_list">
                {% for choice in choices %}
                    <option value="{{ choice }}"></option>
                {% endfor %}
            </datalist>
        </div>
{% endblock %}


// config/packages/twig.yaml

twig:
    paths: ['%kernel.project_dir%/templates']
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    form_themes: ['form/fields.html.twig']

I changed the getParent() method in order to return an EntityType::class

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

Removed the default vaulues for $resolver inside configureOptions() method

public function configureOptions(OptionsResolver $resolver) {

    }

then inside the form builder

->add('degree',DatalistType::class , [
       'label' => 'Choose an master degree',
       'class' => Degree::class
  ])

I expect it works as for the static values, but it didn't.

I've read any kind of question here like

Symfony Forms: HTML5 datalist

but I think the answers posted wasn't complete or it was for old version of Symfony, not for > 3.4

解决方案

The solution is to remove all methods inside DatalistType and leave just constructor and getParent(): EntityType::class

<?php

// path and filename
// /src/form/type/DatalistType.php

namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;  

class DatalistType extends AbstractType {

    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager) {
        $this->entityManager = $entityManager;
    }

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

Then, change the template


{% block datalist_widget %}
<div class="form-group">
    <input {{ block('widget_attributes') }} list="{{ form.vars.id }}_list" value="{{ form.vars.value }}" class="form-control" >
    <datalist id="{{ form.vars.id }}_list">
        {% for choice in choices %}
            <option>
                {{ choice.label }}
            </option>
        {% endfor %}
    </datalist>
</div>
{% endblock %}

It works fine!!!

这篇关于如何为 html5 数据列表定义自定义表单类型类以处理 Symfony 中的 entityType &gt;3.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆