symfony 4 中 getBlockPrefix 的限制值
Restricted values for `getBlockPrefix` in symfony 4
我正在使用 symfony 4 构建一个简单的 CRUD。
我的实体之一叫做 Color
。没什么特别的。它只有 4 个属性:name、description、sortOrder 和 id (PK)。
我还构建了一个表单 class 以便能够 add/edit 我的实体实例。
表格如下所示:
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
class Color extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('id', HiddenType::class)
->add('name', TextType::class)
->add('description', TextareaType::class, ['required' => false])
->add('sort_order', IntegerType::class);
}
}
当我尝试呈现表单时出现错误
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class App\Entity\Color could not be converted to string").
我还有其他看起来相似且有效的表格。
如果我不让 symfony 根据 class 名称决定块前缀并添加方法
public function getBlockPrefix()
{
return 'anything-else-but-color';
}
然后呈现表单。
我不想这样做,因为我的应用程序是基于 "convention" 的,我需要实体 class 名称和表单名称之间的匹配。
我知道我可以同时更改实体和表单 class 名称(我可能会这样做)但这让我很烦恼,因为我不知道会发生什么,也不知道是否还有其他保留字可能搞砸我的应用程序。
附加信息...
我可以部分调试它。
我看到在呈现表单时会生成类似这样的内容
<input type="color" id="color" name="color" class="form-control" ....
(注意字体颜色)。
对于以相同方式构建的不同实体和表单(名为 group
),我将其呈现在与上述相同的位置。
<div id="group">...
所以我的问题是:
- 表格中是否有不能用于
getBlockPrefix
的保留字?
- 我可以使用
color
这个词作为块前缀而不需要跳过很多圈吗?
documentation 涉及到这个:
When the name of your form class matches any of the built-in field
types, your form might not be rendered correctly. A form type named
App\Form\PasswordType
will have the same block name as the built-in
PasswordType
and won't be rendered correctly. Override the
getBlockPrefix()
method to return a unique block prefix (e.g.
app_password
) to avoid collisions.
如果您的表单类型被称为 ColorType
,我预计会触发此事件,但是,为了完整起见,根据您的评论:
I think it crashes for me because the Type at the end of the class
name is ignored.
StringUtil::fqcnToBlockPrefix('Symfony\Component\Form\Extension\Core\Type\ColorType');
and StringUtil::fqcnToBlockPrefix('Whatever\YouWant\Color');
return
the same thing: color
我正在使用 symfony 4 构建一个简单的 CRUD。
我的实体之一叫做 Color
。没什么特别的。它只有 4 个属性:name、description、sortOrder 和 id (PK)。
我还构建了一个表单 class 以便能够 add/edit 我的实体实例。
表格如下所示:
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
class Color extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('id', HiddenType::class)
->add('name', TextType::class)
->add('description', TextareaType::class, ['required' => false])
->add('sort_order', IntegerType::class);
}
}
当我尝试呈现表单时出现错误
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class App\Entity\Color could not be converted to string").
我还有其他看起来相似且有效的表格。
如果我不让 symfony 根据 class 名称决定块前缀并添加方法
public function getBlockPrefix()
{
return 'anything-else-but-color';
}
然后呈现表单。
我不想这样做,因为我的应用程序是基于 "convention" 的,我需要实体 class 名称和表单名称之间的匹配。
我知道我可以同时更改实体和表单 class 名称(我可能会这样做)但这让我很烦恼,因为我不知道会发生什么,也不知道是否还有其他保留字可能搞砸我的应用程序。
附加信息...
我可以部分调试它。
我看到在呈现表单时会生成类似这样的内容
<input type="color" id="color" name="color" class="form-control" ....
(注意字体颜色)。
对于以相同方式构建的不同实体和表单(名为 group
),我将其呈现在与上述相同的位置。
<div id="group">...
所以我的问题是:
- 表格中是否有不能用于
getBlockPrefix
的保留字? - 我可以使用
color
这个词作为块前缀而不需要跳过很多圈吗?
documentation 涉及到这个:
When the name of your form class matches any of the built-in field types, your form might not be rendered correctly. A form type named
App\Form\PasswordType
will have the same block name as the built-inPasswordType
and won't be rendered correctly. Override thegetBlockPrefix()
method to return a unique block prefix (e.g.app_password
) to avoid collisions.
如果您的表单类型被称为 ColorType
,我预计会触发此事件,但是,为了完整起见,根据您的评论:
I think it crashes for me because the Type at the end of the class name is ignored.
StringUtil::fqcnToBlockPrefix('Symfony\Component\Form\Extension\Core\Type\ColorType');
andStringUtil::fqcnToBlockPrefix('Whatever\YouWant\Color');
return the same thing:color