Search code examples
phpformssymfonyinserttwig

How to solve Symfony error Variable "expanded" does not exist?


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?


Solution

  • 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.