Taha Taha - 4 months ago 30
Twig Question

Setting a Quiz System

I'm developing a MCQs based Quiz system where my goal is to assist teacher in adding a new Question and Choices for that question on the same page. According to Symfony documentation, I can embed a Collection of Forms, so I tried embedding ChoiceType to Question form:

->add('answers', CollectionType::class, array(
'entry_type' => ChoiceType::class,
'allow_add' => true,
));
;


Code of new.html.twig page (new question):

<label> Choose answers : </label>
<ul class="tags" data-prototype="{{form_widget(form.answers.vars.prototype)|e('html_attr') }}">
</ul>


But I'm getting empty select input in the browser. Please suggest what could be the perfect solution in this regard?

enter image description here

Note:



I noticed that if I add

use Symfony\Component\Form\Extension\Core\Type\ChoiceType;


to my QuestionType I get the form with an empty select in new.html.twig

when I delete this import I get this error if I open new.html.twig :

Variable "expanded" does not exist in form_div_layout.html.twig at line 38


but I don't have any variable in my entities called 'expanded'

Choice Entity

class Choice
{
...

/**
* @var string
*
* @ORM\Column(name="answer", type="text")
*/
private $answer;

/**
* @var string
*
* @ORM\Column(name="correct", type="string", length=255)
*/
private $correct;


/**
* @var
* @ORM\ManyToOne(targetEntity="ChallengeBundle\Entity\Question",inversedBy="answers")
*/
private $question;

...
}


Question Entity:

class Question
{
...

/**
* @var
* @ORM\OneToMany(targetEntity="ChallengeBundle\Entity\Choice",mappedBy="question",cascade={"remove"})
*/
private $answers;

...
}


Choice Type:

class ChoiceType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('answer')
->add('correct')

;
}

/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'ChallengeBundle\Entity\Choice'
));
}
}

Answer

Thank you all for your Help

I solve my problem by creating another entity It seem that symfony framework find a confusion in the name of my form ChoiceType and Symfony\Component\Form\Extension\Core\Type\ChoiceType