Symfony3: is it possible to change the name of a form?
Solution 1
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
Solution 2
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.
Related videos on Youtube
Roubi
Updated on July 09, 2022Comments
-
Roubi almost 2 years
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.-
Stephan Vierkant about 8 yearsI don't think it's a good idea to rely on a custom form name. Why can't you change your collections? I'm using collections a lot, but I didn't face this problem.
-
Roubi about 8 years@StephanVierkant Why is it a bad idea? If you know of some problem it might lead to, please tell me, as I'm gonna use Matteo's answer. I don't want to change my collections because I have two forms: formA with field myCollection, and formB, which also has a field myCollection. So in my prototype custom form, if I write a block like _formA_myCollection_entry_row, it won't be recognized when I call my formB, because this time it will be called _formB_myCollection_entry_row. Hope I'm clear...
-
-
Roubi about 8 yearsThis is just what I needed! You solved my problem! I didn't know the form name was now called blockPrefix, that's what I didn't find it in the official doc. Thanks for the migration guide link, I'll try to have a look at it next time I'm looking for a pre-3.0 functionality.
-
Roubi about 8 yearsThanks for your answer, I chose Matteo's because it's more appropriate for my particular needs, but I'm sure yours will be helpfull for someone else.
-
chalasr about 8 years@Roubi And you're totally right, it fits your issue exactly
-
Belac over 5 yearsThanks for this answer. I was re-using the same sorta generic form three times on a page and needed a way to have unique ids for the fields. This method worked perfectly for that.