Reputation: 943
I am trying to initialize dynamically a form. The form name is stored in $obj->getFormTypeName().
$formName = 'Acme\testBundle\Form\CustomType\\'.ucfirst($obj->getFormTypeName()).'Type';
$contactForms[$obj->getId()] = $this->createForm($formName::class);
But I get the error : Compile Error: Dynamic class names are not allowed in compile-time ::class fetch
Is there a way to initialize dynamically a form with the Scope Resolution Operator in Symfony ?
Thanks for reading.
Upvotes: 3
Views: 12281
Reputation: 1721
One should not want to call a ::class
on a variable, string name of the class eg.
Acme\testBundle\Form\CustomType\FormType
like this
'Acme\testBundle\Form\CustomType\'::class
, as in the end the string var of the class, it's the classname itself.
Something similar is possible only in Javascript to my knowledge 'stringContents'.val()
Upvotes: 0
Reputation: 1026
To get around this issue, I'd do something like:
$contactForms[$obj->getId()] = $this->createForm(
'Acme\\testBundle\\Form\\CustomType\\' . ucfirst($obj->getFormTypeName()) . 'Type'
);
After all, ::class only returns the fully qualified name of the class.
Upvotes: 8