Symfony3: is it possible to change the name of a form?

雨燕双飞 提交于 2019-11-29 03:30:40
Matteo

You should implements the getBlockPrefix method instead of getName as described in the migration guide here.

As example:

/**
 * 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";
}

Hope this help

Depending on how your form is built, there is different ways to set the name of your form.

If you are creating the form through $this->createForm(CustomType::class):

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

If you are building the form from the controller directly through $this->createFormBuilder():

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

Look at the FormFactory and FormBuilder APIs for more information.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!