Reputation: 212
Currently, I develop an app with PHP Symfony Framework. I've got a problem with Form Builder (I think).
I have two entities. Question and Choice. Question and Choice are OneToMany Relationship Entity. One Question has many Choice.
Another two entities, Video and Category, the relationship is just the same with Question and Choice.
I create scaffolding crud for those entity with php bin/console make:crud
.
Then I add the relationship symfony like in this guide from Symfony.
The logic is, I must select the Category first to create new Video. Same with the Choice, I must select the Question first to create new Choice data.
My problem appear when I open the Choice Create Form [/choice/new]. It says
Variable "expanded" does not exist.
Then the error details show on this lines
return $this->render('choice/new.html.twig', [
'choice' => $choice,
'form' => $form->createView(), // The highlighted error appear on this line
]);
But, It just happen in the Question-Choice, My Category-Video relationship is just fine. I tried to make Question-Choice as same as Category-Video (I changed the name of the entity for sure), I triple check it, but the error on Choice Create Form still occur.
This is my App\Form\ChoiceType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('content')
->add('letter')
->add('image')
->add('question', EntityType::class, [
'class' => Question::class,
'choice_label' => 'content'
])
;
}
Notice the add('question')
and this is my App\Form\VideoType buildForm method
<?php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('url', FileType::class, [
'label' => 'Video File',
'required' => false,
])
->add('thumbnail', FileType::class, [
'required' => false,
])
->add('description')
->add('category', EntityType::class, [
'class' => Category::class,
'choice_label' => 'name'
])
;
}
Notice the add('category')
So, anyone know what is happening?
Upvotes: 0
Views: 1052
Reputation: 143
Renaming certainly will work, because error is name-related.
In your form class App\Form\ChoiceType add method getBlockPrefix
like this:
class ChoiceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
// ………
}
public function getBlockPrefix()
{
return 'question_choice';
}
// ………
}
This way your form won't hit {%- block choice_widget -%}
in form layout which is rendered with default block prefix.
Default block prefix is created based on form class name. You can read more about form fragment naming here:
https://symfony.com/doc/current/form/form_themes.html#form-fragment-naming
The same goes for others form names, like text, textarea, file, even color (the same problem Chloé had).
Naming things is hard sometimes :)
Upvotes: 1
Reputation: 212
I renamed App\Form\ChoiceType to App\Form\TheChoiceType, make some adjusment for class name changing on the controller. Everything is work!
I don't believe this! The solution is to rename the form type.
Upvotes: 3