Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将自定义参数/属性从细枝模板传递到自定义表单字段?_Php_Symfony_Twig_Symfony Forms - Fatal编程技术网

Php 如何将自定义参数/属性从细枝模板传递到自定义表单字段?

Php 如何将自定义参数/属性从细枝模板传递到自定义表单字段?,php,symfony,twig,symfony-forms,Php,Symfony,Twig,Symfony Forms,我正在使用Symfony 3.4并创建了一个自定义FormType。我想将一些参数从细枝模板传递给这种类型的表单字段 根据合同,这应该是可能的: {# render a widget, but add a "foo" class to it #} {{ form_widget(form.name, { 'attr': {'class': 'foo'} }) }} 虽然这在使用内置表单类型(如TextType时有效,但我无法找到如何为自定义类型实现相同的功能: // For

我正在使用Symfony 3.4并创建了一个自定义FormType。我想将一些参数从细枝模板传递给这种类型的表单字段

根据合同,这应该是可能的:

{# render a widget, but add a "foo" class to it #}
{{ form_widget(form.name, { 'attr': {'class': 'foo'} }) }} 
虽然这在使用内置表单类型(如
TextType
时有效,但我无法找到如何为自定义类型实现相同的功能:

// Form Type class
class MyCustomType extends AbstractType {

    public function getBlockPrefix() {
        return 'my_custom_type';
    }

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

    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults(array(
            'translation_domain' => 'app',
        ));
    }
}


// Widget Template
{% block my_custom_type_widget %}
    {{ dump(form) }}
    {% if form.vars.attr.class is defined %} 
        {{ form.vars.attr.class }}
    {% endif %}
    ...
{% endblock %}


// Form template
...
{{ form_start(form) }}
    {{ form_widget(form.myCustom, {'attr': {'class': 'customClass'}}) }}
    ...
{{ form_end(form) }}
表单模板中添加的
customClass
不会传递到小部件模板。
{%if form.vars.attr.class已定义%}
测试有效,但只有默认类
formName myCustom
可用


那么,如何将自定义属性从twig传递到自定义表单类型?我必须对我的自定义类型进行哪些更改才能使其像在Twig中传递类一样工作?

尝试将默认类与中的新类合并,如中所示。

是否尝试将默认类与新类合并,如中所示?谢谢@qdequipe。这不是完全正确的答案,但非常接近,它让我走上了正确的轨道:从细枝模板传递的参数不能通过
form.vars.attr
访问,而是直接通过
attr
访问。如果你发布一个答案,我可以接受。。。