使用 Symfony 2.7 ,您可以使用方法getName()
在您的EntityType类中自定义表单名称
现在已弃用。还有另一种方法可以用 Symfony 3.0 来做到这一点吗?
我有需要以不同形式使用的集合的自定义原型entry_rows
由于行的名称基于表单的名称,因此我需要更改后者以便以不同的形式使用它们。
答案 0 :(得分:25)
您应该按照迁移指南here中的说明实施getBlockPrefix
方法,而不是getName
。
例如:
/**
* Returns the prefix of the template block name for this type.
*
* The block prefix defaults to the underscored short class name with
* the "Type" suffix removed (e.g. "UserProfileType" => "user_profile").
*
* @return string The prefix of the template block name
*/
public function getBlockPrefix()
{
return "form_name";
}
希望这个帮助
答案 1 :(得分:22)
根据表单的构建方式,有不同的方法来设置表单的名称。
如果您通过$this->createForm(CustomType::class)
创建表单:
$formFactory = $this->get('form.factory');
$form = $formFactory->createNamed('custom_form_name', CustomType::class);
如果您是通过$this->createFormBuilder()
直接从控制器构建表单:
$formFactory = $this->get('form.factory');
$form = $formFactory->createNamedBuilder('custom_form_name', CustomType::class);
查看FormFactory和FormBuilder API以获取更多信息。
答案 2 :(得分:0)
您可以尝试一下,删除字段名称上的前缀
public function getBlockPrefix()
{
return null;
}