简体   繁体   English

我如何用 <div> 与Symfony formbuilder一起使用时的标签?

[英]How can I wrap my input field with a <div> tag when using with Symfony formbuilder?

I am creating an input field with Symfony: 我正在用Symfony创建一个输入字段:

$formBuilder->add($field['fieldName'], TextType::class, array('attr' => array('class' => 'form-control')));

The output is: 输出为:

<input type="text" id="form_color" name="form[color]" class="form-control">

The output I would need is: 我需要的输出是:

<div class="cp input-group colorpicker-component">
<input type="text" id="form_color" name="form[color]" class="form-control" /> <span class="input-group-addon"><i></i></span>
</div>

This means I somehow have to add a parent to the specific form field. 这意味着我必须以某种方式将父项添加到特定的表单字段。 I cannot add it to the actual form, because this parent div is only added to the field in specific cases. 我无法将其添加到实际表单中,因为此父div仅在特定情况下才添加到字段中。 Is there a way to do it in the formbuilder? 有没有办法在formbuilder中做到这一点?

I'd create my own custom field type to handle these fields. 我将创建自己的自定义字段类型来处理这些字段。

First define the field class 首先定义字段类

// src/Form/Type/ColorSelectorType.php
namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

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

Then you'll need to create the template for the field: 然后,您需要为该字段创建模板:

{# templates/form/fields.html.twig #}
{% block color_selector_widget %}
    {% spaceless %}
       <div class="cp input-group colorpicker-component">
            <input{{ block('widget_container_attributes') }} />
                 <span class="input-group-addon"><i></i></span>
       </div>
    {% endspaceless %}
{% endblock %}

Register your custom templates: 注册您的自定义模板:

# config/packages/twig.yaml
twig:
    form_themes:
        - 'form/fields.html.twig'

And finally use your new field: 最后使用您的新字段:

$formBuilder->add($field['fieldName'], ColorSelectorType::class);

Logically, you'll need to customize this to suit your needs, but it should be enough to get you going. 从逻辑上讲,您需要自定义此选项以适合您的需求,但这足以使您前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改FormBuilder DateType字段的格式(Symfony 4)? - How can I change the format of my formbuilder DateType field (Symfony 4)? 如何转换我的日期 object 以在 Symfony formbuilder 中使用它? - How can I convert my date object to use it in Symfony formbuilder? 使用query_builder时,如何在Symfony formbuilder中选择默认选项? - How can I select a default option with Symfony formbuilder when using query_builder? 如何在 Symfony 4 中将 queryBuilder 与 formBuilder 一起使用? - How can I use the queryBuilder with formBuilder in Symfony 4? 如何使用Symfony 2 FormBuilder禁用编辑视图中的字段 - How to disable a field in edit view using Symfony 2 FormBuilder Symfony 4 - 无法为 formBuilder 中的表单字段自定义 attr - Symfony 4 - Can not customize attr for form field in formBuilder 如何使用带有foreach循环的Symfony formbuilder存储内容? - How can I store content with Symfony formbuilder with foreach loop? 如何使用 Symfony formbuilder 仅获取实体的特定元素? - How can I get only specific elements of an entity with Symfony formbuilder? 如何使用Symfony formbuilder设置表单名称? - How can I set the form name with Symfony formbuilder? 如何在服务 (Symfony4) 中解析 FormBuilder? - How can I resolve FormBuilder inside a service (Symfony4)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM