N
N
nnkrasovok2020-09-23 23:31:06
symfony
nnkrasovok, 2020-09-23 23:31:06

How to write to the database the value of the radiobutton if it is not selected?

there is a form for adding a question with multiple answers. radiobutton indicates the correct / incorrect answer

form of the question:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('text', TextType::class, [
                'label' => 'Question'
            ])
            ->add('answers', CollectionType::class, [
                'entry_type' => AnswerType::class,
                'label' => false,
                'by_reference' => false,
                'allow_add' => true,
                'allow_delete' =>true,
            ])
            ->add('save', SubmitType::class, [
                'attr' => [
                    'class' => 'btn btn-success'
                ]
            ])
        ;
    }


response form:
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('is_true', RadioType::class)
            ->add('text', TextType::class)
        ;
    }


controller:
public function addQuestion(Request $request)
    {
        $forRender = parent::renderDefault();
        $forRender['title'] = 'Add Question';

        $em = $this->getDoctrine()->getManager();
        $question = new Question();

        $form = $this->createForm(QuestionType::class, $question);

        $form->handleRequest($request);

        if($form->isSubmitted()){
            $em->persist($question);
            $em->flush();
            $this->addFlash('notification', 'Question added');
            return $this->redirect($this->generateUrl("question"));
        }

        $forRender['form'] = $form->createView();

        return $this->render('admin/createquestion.html.twig', $forRender);
    }


only the correct answer option is written to the database (that is, the radiobutton is selected). how to make it so that another answer option is recorded, which is incorrect (radiobutton is not selected).

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question