简体   繁体   中英

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

With Symfony 2.7 , you could customize a form's name in your EntityType class with the method getName()
This is now deprecated. Is there another way to do that with Symfony 3.0 ?
I have custom prototype entry_rows for collections that I would need to use in different forms.
Since the name of the rows is based on the form's name, I would need to change the later in order to use them with a different form.

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

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.

You can try it, remove prefix on field name

public function getBlockPrefix()
{
    return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM