如何根据一个简单的规则设置所有实体表单的所有标签

发布于 2024-12-11 14:00:59 字数 149 浏览 0 评论 0原文

对于我的 Symfony2 应用程序的所有实体形式,我希望所有标签都采用以下形式: . 之后能够更改标签会很酷。

我怎样才能实现这个目标?

for all the entity forms of my Symfony2 app, I'd like all labels to be of the following form: <entity_name>.<field_name>
Having the possibility to change the label afterwards would be cool.

How can I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蘸点软妹酱 2024-12-18 14:00:59

默认标签的行为在 FieldType 中定义:

https://github.com/symfony/symfony/blob/2.0/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php#L55

(注:FieldType 类在 Symfony 2.1 中已弃用,并在 Symfony 2.3 中删除)

如果您想更改默认类型的行为,您可以创建一个扩展:

https://github.com/symfony/symfony /blob/master/src/Symfony/Component/Form/AbstractTypeExtension.php

class FieldTypeEntityLabelExtension
{
    static $currentTypeName;
    public function buildForm(FormBuilder $builder, array $options)
    {
        $types = $builder->getTypes();

        $type = end($types);

        $dataClass   = get_class($type);
        if (strpos($dataClass, 'Uc') === 0) // <-- My namespace
        {
            $classParts              = explode( '\\', $dataClass);
            $translationParts        = array_slice($classParts, 3);
            self::$currentTypeName   = implode('.', array_map('Symfony\Component\DependencyInjection\Container::underscore', $translationParts));
        }

        if (isset(self::$currentTypeName) && !$options['precious'])
        {
            $builder->setAttribute('label', sprintf('%s.%s', substr(self::$currentTypeName, 0, -5), $builder->getName()));
        }
    }

    public function getExtendedType()
    {
        return 'field';
    }
}

The default label's behavior is defined in the FieldType:

https://github.com/symfony/symfony/blob/2.0/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php#L55

(note: the FieldType class was deprecated in Symfony 2.1 and removed in Symfony 2.3)

If you want to change the default type's behavior, you can create an extension:

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/AbstractTypeExtension.php

class FieldTypeEntityLabelExtension
{
    static $currentTypeName;
    public function buildForm(FormBuilder $builder, array $options)
    {
        $types = $builder->getTypes();

        $type = end($types);

        $dataClass   = get_class($type);
        if (strpos($dataClass, 'Uc') === 0) // <-- My namespace
        {
            $classParts              = explode( '\\', $dataClass);
            $translationParts        = array_slice($classParts, 3);
            self::$currentTypeName   = implode('.', array_map('Symfony\Component\DependencyInjection\Container::underscore', $translationParts));
        }

        if (isset(self::$currentTypeName) && !$options['precious'])
        {
            $builder->setAttribute('label', sprintf('%s.%s', substr(self::$currentTypeName, 0, -5), $builder->getName()));
        }
    }

    public function getExtendedType()
    {
        return 'field';
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文