我们可以通过Symfony控制器以表单的形式定义小部件的id属性吗? [英] Can we define the id attribute of a widget in a form from a Symfony controller?

查看:58
本文介绍了我们可以通过Symfony控制器以表单的形式定义小部件的id属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以表单的形式定义小部件的 id属性使用表单类.

I'd like to define the id attribute of a widget in a form using a form class.

从Twig视图中, Symfony文档向我们展示如何添加"foo"类属性:

From a Twig view, the Symfony documentation show us how to add a "foo" class attribute:

{{ form_widget(form.surname, {'attr': {'class': 'foo'}}) }}

这可以使用以下方法在表单类中完成:

This can be done from the form class using:

$builder->add('surname', TextType::class, [
    'attr' => ['class' => 'foo'],
])

因此,如果我们可以像这样更改class属性,则可以假设我们可以像以下那样更改id属性:

So, if we can change the class attribute like this, we could suppose we can change the id attribute like the following:

{{ form_widget(form.title, { 'attr': {'id': 'my_custom_id'} }) }}

但是这不起作用,我们需要在attr数组之外定义id,如下所示:

However this does not work, we need to define the id outside of the attr array like the following:

{{ form_widget(form.surname, { 'id': 'my_custom_id' }) }}

基于这种观察,如果我尝试在表单类的attr之外定义id,则如下所示:

Based on that observation, if I try to define the id outside of the attr from the form class like this:

$builder->add('surname', TextType::class, [
    'id' => 'my_custom_id',
])

但这是不允许的,我得到以下信息:

But this is not allowed, I get the following:

选项"id"不存在.定义的选项为:操作","allow_extra_fields","attr","auto_initialize","block_name","by_reference","compound","constraints","csrf_field_name","csrf_message","csrf_protection","csrf_token_id ","csrf_token_manager",数据","data_class",已禁用","empty_data","error_bubbling","error_mapping","extra_fields_message","inherit_data","invalid_message","invalid_message_parameters","label" "label_attr","label_format","mapped","method","post_max_size_message","property_path","required","translation_domain","trim","upload_max_size_message","validation_groups".

The option "id" does not exist. Defined options are: "action", "allow_extra_fields", "attr", "auto_initialize", "block_name", "by_reference", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "inherit_data", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "mapped", "method", "post_max_size_message", "property_path", "required", "translation_domain", "trim", "upload_max_size_message", "validation_groups".

我注意到我们无法为

I noticed we can't define the id inside of the attr array for a widget while we can do it inside of the label_attr array for a label, even from the form class.

是否可以通过表单类定义窗口小部件的ID?如果是这样,该怎么做?

Is it possible to define the id of a widget from a form class? If so, how to do it?

编辑:我想说的是,我测试了以下内容,仅更改了标签的ID.

I'd like to mention that I tested the following and only the id of the label changed.

->add('surname', TextType::class, [
    'label_attr' => ['id' => 'my_custom_id1'],
    'attr' => ['id' => 'my_custom_id2'],
])

我正在使用Symfony v3.3.6

I'm using Symfony v3.3.6

EDIT2 :我创建了一个沙盒项目,该项目可再现问题.

推荐答案

事实上,不可能作为Symfony

In fact, it's not possible as Symfony generates ids for form types it renders.

此外,这是.

In addition this is unlikely to change in the core as that would also be a huge BC break to not generate it anymore.

我们只能通过覆盖$view->vars['id']来更改ID的前缀,如下所示:

We can only change the prefix of the id by overriding the $view->vars['id'] like the following:

public function buildView(FormView $view, FormInterface $form, array $options)
{

    $view->vars['id'] = 'myprefix';
}

后缀是$builder->add('surname', TextType::class)中定义的名称.在这种情况下,新的ID为myprefix_surname.

The suffix is the name defined in $builder->add('surname', TextType::class). In this case the new id would be myprefix_surname.

这篇关于我们可以通过Symfony控制器以表单的形式定义小部件的id属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆