简体   繁体   English

Symfony 3+:自定义表单元素模板

[英]Symfony 3+: Custom form element template

We are finally upgrading to Symfony 3 which however causes some issues. 我们最终将升级到Symfony 3,但这会引起一些问题。

Current problem I am trying to solve is https://api.symfony.com/2.8/Symfony/Component/Form/FormTypeInterface.html#method_getName witch has been deprecated. 我要解决的当前问题是https://api.symfony.com/2.8/Symfony/Component/Form/FormTypeInterface.html#method_getName女巫已被弃用。

FormTypeInterface#getName() was used to create a nickname witch was used in twig form templates. FormTypeInterface#getName()用于创建昵称,在树枝表单模板中使用。

Example custom form type: 自定义表单类型示例:

class MyCustomType extends AbstractType {

    // ...

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

}

Example template row in Twig: Twig中的示例模板行:

{% block my_custom_row %}

    {# ... #}

{% endblock %}

Question: 题:

What is the naming convention for custom form types in Symfony 3+? Symfony 3+中自定义表单类型的命名约定是什么? Ie how should I name the twig blocks to customize the form row/widget/error rendering? 即我应该如何命名树枝块以自定义表单行/窗口小部件/错误呈现?

Twig block by default should be named as FormType class without Type suffix. 默认情况下,Twig块应命名为不带Type后缀的FormType类。 ie. 即。 my_custom_row for *_row block. my_custom_row*_row块。

Other, more custom way is to add function getBlockPrefix() ( https://api.symfony.com/3.1/Symfony/Component/Form/FormTypeInterface.html#method_getBlockPrefix ). 其他更自定义的方法是添加函数getBlockPrefix()https://api.symfony.com/3.1/Symfony/Component/Form/FormTypeInterface.html#method_getBlockPrefix )。 That function should return exactly the same block name prefix as getName() before, but this is optional and require only if block prefix is different than class name. 该函数应该返回与getName()之前完全相同的块名前缀,但这是可选的,并且仅当块前缀与类名不同时才需要。

In Symfony4 is also available form option block_name that override defaults. 在Symfony4中,也可以使用覆盖默认值的选项block_name形式。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM