Symfony3:是否可以更改表单的名称?

Rou*_*ubi 16 php forms symfony-forms symfony

使用Symfony 2.7,您可以使用方法自定义EntityType类中的表单名称getName()
.现在不推荐使用.是否有另一种方法可以使用Symfony 3.0
我有自定义原型entry_rows的集合,我需要以不同的形式使用.
由于行的名称基于表单的名称,因此我需要更改后者才能使用不同的表单.

Mat*_*teo 28

您应该实现该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";
}
Run Code Online (Sandbox Code Playgroud)

希望这有帮助


cha*_*asr 22

根据表单的构建方式,有不同的方法来设置表单的名称.

如果您通过$this->createForm(CustomType::class)以下方式创建表单:

$formFactory = $this->get('form.factory');
$form = $formFactory->createNamed('custom_form_name', CustomType::class);
Run Code Online (Sandbox Code Playgroud)

如果要直接通过$this->createFormBuilder()以下方式从控制器构建表单:

$formFactory = $this->get('form.factory');
$form = $formFactory->createNamedBuilder('custom_form_name', CustomType::class);
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请查看FormFactoryFormBuilder API.