Asking for User Consent in Symfony Form doesn’t submit

this is my first post on stackoverflow !

I have this problem in my Controller method.
I would like to ask the User if he wants to add something that he searched for and doesn’t exist to the database. But when I ask for his consent, the form I made doesn’t submit. I’m not sure if I’m clear or not but here’s my 2 forms first and the controller function.

The first form I use :

namespace AppForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;

class MachineOSType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name',TextType::class, [
                'label' => 'Nom de l'OS'
            ])
            ->add('version',TextType::class, [
                'label' => 'Version de l'OS',
                'required' => false
            ])
            ->add('Valider',SubmitType::class, [
                'attr' => ['class' => 'btn btn-block form-rounded btn-success']
            ])
    ;
}
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => null
    ]);
}
}

The Second form asking for consent, just a yes/no (oui/non)

namespace AppForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeChoiceType;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;

class ValidationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('reponse',ChoiceType::class,[
                    'label' => ' Choix ',
                    'choices'=> [
                        'Oui' => 1,
                        'Non' => 0,
                    ],
                    'expanded' => true,
                    'multiple' => false,
                    'label_attr' => ['class' => 'radio-inline text-center '],
                    'attr' => ['class' => 'text-center']
                ])
            ->add('Valider',SubmitType::class, [
                'attr' => ['class' => 'btn btn-block btn-success form-rounded']
            ])
    ;
}
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => null
    ]);
}
}

The Controller :

/**
 * @Route("/{id}/OS/add", name="os_machine_add_form", methods={"GET","POST"})
 */
public function os_machine_add_form(Machine $machine, Request $request, Request $request2, OperatingSystemRepository $osr)
{
    $em = $this->getDoctrine()->getManager();
    $form = $this->createForm(MachineOSType::class);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $nomOS = $form["name"]->getData();
        $verOS = $form["version"]->getData();
        $fullOS = [
            'nomOS' => $nomOS,
            'verOS' => $verOS
        ];
        if ($verOS != null) {
            $OS = $osr->findBy([
                'name' => $nomOS,
                'version' => $verOS,
            ]);
            $size = count($OS);
            if ($size == 0) {
                $form2 = $this->createForm(ValidationType::class);
                $form2->handleRequest($request2);
                if ($form2->isSubmitted() && $form2->isValid()) {
                    if ($form2["reponse"]->getData() == 1) {
                        $newOS = new OperatingSystem();
                        $newOS->setName($nomOS);
                        $newOS->setVersion($verOS);
                        $em->persist($newOS);
                        $em->flush();
                    } 
                }
                return $this->render('machine/os_add.html.twig', [
                    'fullos' => $fullOS,
                    'os' => $OS,
                    'machine' => $machine,
                    'form' => $form->createView(),
                    'form2' => $form2->createView(),
                ]);
            }
        }
    }
    return $this->render('machine/os_add.html.twig', [
        'machine' => $machine,
        'form' => $form->createView(),
    ]);
}

Here is what I get when I submit something that doesn’t exist in the database:
Image output
but when I click on “Valider” It doesn’t create a new object.

If you have any suggestions, or any other way of doing that, I’m taking it, I’m kinda new to Symfony so 🙂

Thank you for reading.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *