Form no submitting with isValid()

198 Views Asked by At

I have an issue with a form in a project. I cannot pass the isValid for this one meanwhile other forms don't have this issue. I don't what to do, I'm a stuck. Don't hesitate to guide me or to give hints. Here are the code excerpt:

controller:

<?php

namespace App\Controller;

use App\Entity\ChangementObjetSocial;
use App\Entity\Commande;
use App\Form\AccountType;
use App\Form\ChangementObjetSocialType;
use App\Form\CommandeType;
use App\Security\LoginFormAuthenticator;
use App\Service\CustomMailer;
use App\Service\UserAuto;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Snappy\Pdf;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response};
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Twig\Environment;
use Twig\Error\{LoaderError, RuntimeError, SyntaxError};

class ChgtObjetSocialController extends AbstractController
{
    //CRUD REDACTION_LIBRE

    /**
     * @var Environment
     */
    private $twig;
    private $pdf;

    /**
     * SarlController constructor.
     * @param Environment $twig
     * @param Pdf $pdf
     */
    public function __construct(Environment $twig, Pdf $pdf)
    {
        $this->twig = $twig;
        $this->pdf = $pdf;
    }

    // CREATE

    /**
     * @Route("/changement_objet_social_creation", name="changement_objet_social_creation")
     * @param Request $request
     * @param EntityManagerInterface $manager
     * @param UserPasswordEncoderInterface $encoder
     * @param MailerInterface $mailer
     * @param GuardAuthenticatorHandler $guardHandler
     * @param LoginFormAuthenticator $authenticator
     * @return RedirectResponse|Response
     */
    public function changement_objet_social_creation(Request $request, EntityManagerInterface $manager,
                                                     UserPasswordEncoderInterface $encoder, MailerInterface $mailer,
                                                     GuardAuthenticatorHandler $guardHandler,
                                                     LoginFormAuthenticator $authenticator)
    {
        $chgt_objet_social = new ChangementObjetSocial();

        $user = $this->getUser();

        $form = $this->createForm(ChangementObjetSocialType::class, $chgt_objet_social);
        $form->handleRequest($request);

        $formUser = $this->createForm(AccountType::class, $user);
        $formUser->handleRequest($request);

        if($form->isSubmitted() && $form->isValid())
        {
            // User account created if no existing account found

            if ($user == null) {
                $userAuto = new UserAuto();
                $userAuto->userAuto($request, $encoder, $guardHandler, $authenticator, $manager, $formUser);

                // Get the user
                $user = $userAuto->getUser();
                $mdp = $userAuto->getMdp();

                //Sending him an e-mail with his credentials in order to let him change pw
                $customMailer = new CustomMailer();
                $customMailer->automaticAccount($user, $mdp, $mailer);
            }

            $chgt_objet_social->setUser($user);
            $chgt_objet_social->setCreatedAt(new \DateTime('now'));

            $manager->persist($chgt_objet_social);
            $manager->flush();

            ///////////////////////////////////// MAKING COMMANDE
            /// RETRIEVING PRIME DATAS FOR ANNONCE
            $denomination  = $chgt_objet_social->getDenomination();
            $rcs           = $chgt_objet_social->getRcs();
            $adresse       = $chgt_objet_social->getAdresse();
            $code_postal   = $chgt_objet_social->getCodePostal();
            $date_creation = $chgt_objet_social->getCreatedAt();
            $date          = date_format($date_creation, 'd-m-Y');
            $annonce       = "Par Acte la société " . $denomination . " située à l'adresse " .
                             $adresse . " " . $code_postal . " dont le RCS est " . $rcs .
                             " dont le RCS est " . $rcs.". Annnonce créee le " . $date ;

            /////////////////////////////////////INSTANCE OF COMMANDE AND FLUSHING
            //
            $commande     = new Commande();
            $commande->setUser($user);
            $commande->setAEffectuer(0);
            $commande->setPayee(0);
            $commande->setCreatedAt(new \DateTime('now'));
            $commande->setChgtObjetSocial($chgt_objet_social);
            $commande->setAnnonce($annonce);
            $commande->setPrix(20); // Temporary price
            $manager->persist($commande);
            $manager->flush();

            ////////////////////////////////////

            $this->addFlash(

                'success', "Félicitations ! Veuilez vérifier l'annonce puis procédez au <strong> paiement</strong>."
            );

            return $this->redirectToRoute('changement_objet_social_visualiser', [
                'chgt_objet_social' => $chgt_objet_social->getId(),
                'commande'          => $commande->getId(),
                'form'              => $form->createView(),
            ]);
        }

        if(!$user){
            $this->addFlash(
                'info',
                'Si vous possédez un compte chez Modèle Annonce Légale, veuillez-vous connecter avant de continuer'
            );
        }

        return $this->render('chgt_objet_social/chgt_objet_social_creer.html.twig', [
            'form'      => $form->createView(),
            'form_user' => $formUser->createView(),
        ]);
    }

    // READ

    /**
     * @Route("/changement_objet_social_visualiser/{chgt_objet_social}/{commande}", name="changement_objet_social_visualiser")
     * @param ChangementObjetSocial $chgt_objet_social
     * @param Commande $commande
     * @param EntityManagerInterface $manager
     * @param Request $request
     * @return Response
     * @throws LoaderError
     * @throws RuntimeError
     * @throws SyntaxError
     */
    public function chgt_objet_social_visualiser(ChangementObjetSocial $chgt_objet_social,
                                                 Commande $commande, EntityManagerInterface $manager,
                                                 Request $request)
    {
        $form = $this->createForm(CommandeType::class, $commande);
        $user = $this->getUser();

        if ($request->isMethod('POST')){
            $form->submit($request->request->get($form->getName()));
        }

        if($form->isSubmitted() && $form->isValid())
        {
            $data = $form->getData();
            $commande->setAnnonce($data->getAnnonce());
            $manager->flush();

            if ($form->getClickedButton() && 'modify' === $form->getClickedButton()->getName()) {
                return $this->redirectToRoute('changement_objet_social_modifier', [
                    'id'       => $chgt_objet_social->getId(),
                    'commande' => $commande->getId(),
                    'form'     => $form->createView()
                ]);
            }

            if ($form->getClickedButton() && 'estimate' === $form->getClickedButton()->getName())
            {
                $commande->setAEffectuer(1);
                $manager->flush();
                $numero_de_devis = $commande->getId();
                $date_pdf = date('d-m-Y');
                $html = $this->twig->render('email/devis.html.twig', [
                    'numero_de_commande' => $commande->getId(),
                    'prix_ht'            => $commande->getPrix()*0.8,
                    'ht'                 => $commande->getPrix()*0.2,
                    'prix_ttc'           => $commande->getPrix(),
                    'annonce'            => $commande->getAnnonce(),
                    'user'               => $user,
                ]);
                return new Response(
                    $pdf = $this->pdf->getOutputFromHtml($html),
                    200,
                    array(
                        'Content-Type'          => 'application/pdf',
                        'Content-Disposition'   => 'attachment; filename="devis-'.$numero_de_devis.'-modele_annonce_legale-'.$date_pdf.'.pdf"'
                    )
                );
            }

            if ($form->getClickedButton() && 'payment' === $form->getClickedButton()->getName()) {

                return $this->redirectToRoute('checkout', [
                    'forme'        => $chgt_objet_social->getId(),
                    'id'           => $commande->getId(),
                    'commande'     => $commande,
                    'form'         => $form->createView(),
                ]);
            }
        };


        return $this->render('chgt_objet_social/chgt_objet_social_visualiser.html.twig', [
            'chgt_objet_social' => $chgt_objet_social,
            'commande'          => $commande,
            'form'              => $form->createView()
        ]);
    }

    /**
     * @Route("/chgt_objet_social/chgt_objet_social_modifier/{id}/{commande}", name="changement_objet_social_modifier")
     * Method({"POST"})
     * @Security("is_granted('ROLE_USER') and user === chgt_objet_social.getUser()", message="Cette annonce ne vous appartient pas. Il vous est impossible de la modifier.")
     * @param ChangementObjetSocial $chgt_objet_social
     * @param Commande $commande
     * @param EntityManagerInterface $manager
     * @param Request $request
     * @return RedirectResponse|Response
     */
    public function chgt_objet_social_modifier(ChangementObjetSocial $chgt_objet_social, Commande $commande, EntityManagerInterface $manager, Request $request)
    {
        $form = $this->createForm(ChangementObjetSocialType::class, $chgt_objet_social);
        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid())
        {
            $manager->flush();
            $this->addFlash(
                'success',
                'L\'annonce a bien été modifiée.'
            );
            return $this->redirectToRoute('changement_objet_social_visualiser', [
                'chgt_objet_social' => $chgt_objet_social->getId(),
                'commande'          => $commande->getId(),
                'form'              => $form->createView(),
            ]);
        };
        return $this->render('chgt_objet_social/chgt_objet_social_modifier.html.twig', [
           'id'       => $chgt_objet_social,
           'commande' =>$commande->getId(),
           'form'     => $form->createView()
        ]);

    }
}

and here is the problematic twig view:

{% extends 'base.html.twig' %}

{% block title %}
    CHANGEMENT OBJET SOCIAL
{% endblock %}

{% block stylesheets %}
    {{ parent() }}

    {{ encore_entry_link_tags('chgt_objet_social_creer') }}

{% endblock %}

{% block body %}

    <div class="progress-container ml-1">
        <br/>
        <small class="ml-1">Progression</small>
        <div class="progress-bar ml-1" id="myBar"></div>
    </div>

    <div class="wrapper col-7 mx-auto">
        <div class="contact-clean">
            {{ form_start(form) }}
            <form method="post">
                <h1 class="text-center text-focus-in">CHANGEMENT D'OBJET SOCIAL <i class="qtip tip-right" data-tip=" Important ! Connectez-vous au préalable si vous avez un déjà un compte chez Modèle Annonce Légale sinon ce formulaire vous permettra de créer un compte." style="z-index: 2; font-size: 0.6em;">
                        <span class="ion-ios-help-outline"></span></i>
                </h1>
                <hr>
                <div class="form-group">
                    <p>EN-TÊTE</p>
                    <hr>
                </div>
                <div class="form-group">
                    <em class="isBlue-text">exemple: "CABINET D'AVOCAT, 36 rue de l'est, Paris 75001"</em>
                </div>
                <div class="form-check form-group">
                    <input class="form-check-input entete" type="checkbox" id="entete" value="option1">
                    <label class="form-check-label" for="entete">Avec un en-tête</label>
                </div>

                <div class="form-group" id="hidden_entete">
                    {{ form_widget(form.en_tete) }}
                </div>

                <br/>
                <br/>

                <div class="la_decision">

                    <div class="form-group">
                        <p>LA DECISION</p>
                        <hr>
                    </div>

                    <div class="form-group form-inline">
                        <div class="standard-form-width">
                        {{ form_label(form.organisme_de_decision) }}
                        </div>
                        {{ form_widget(form.organisme_de_decision) }}

                    </div>

                    <div class="form-group form-inline">
                        <div class="standard-form-width">
                        {{ form_label(form.date_decision, 'Date de la décision') }}
                        </div>
                        {{ form_widget(form.date_decision) }}
                        <span class="text-danger"> <strong>{{ form_errors(form.date_decision) }}</strong></span>

                    </div>

                    <div class="form-group form-inline">
                        <div class="standard-form-width">
                        {{ form_label(form.date_d_effet) }}
                        </div>
                        {{ form_widget(form.date_d_effet) }}

                    </div>

                </div>

                <br/>
                <br/>

                <div class="societe">

                    <div class="form-group">
                        <p>LA SOCIETE</p>
                        <hr>
                    </div>

                    <div class="form-group form-inline">
                        <div class="standard-form-width">
                        {{ form_label(form.denomination, 'Dénomination de la société') }} &nbsp;
                        <i class="qtip tip-right" data-tip="Nom de société tel que mentionnez dans les statuts (en majuscule).">
                            <span class="ion-ios-help-outline"></span>
                        </i>
                        </div>
                        {{ form_widget(form.denomination) }}
                        {{ form_errors(form.denomination) }}

                    </div>

                    <div class="form-group form-inline">
                        <div class="standard-form-width">
                        {{ form_label(form.forme, 'Forme juridique') }}
                        </div>
                        {{ form_widget(form.forme) }}

                    </div>

                    <div class="form-group form-inline">
                        <div class="standard-form-width">
                        {{ form_label(form.capital, 'Capital') }}
                        </div>
                        {{ form_widget(form.capital) }}

                    </div>

                    <div class="form-group form-inline">
                        <div class="standard-form-width">
                        {{ form_label(form.adresse, 'Adresse') }}
                        </div>
                        {{ form_widget(form.adresse) }}

                    </div>

                    <div class="form-group form-inline">
                        <div class="standard-form-width">
                        {{ form_label(form.adresse_suite, 'Adresse suite') }}
                        </div>
                        {{ form_widget(form.adresse_suite) }}

                    </div>

                    <div class="form-group form-inline">
                        <div class="standard-form-width">
                        {{ form_label(form.code_postal, 'Code postal') }}
                        </div>
                        {{ form_widget(form.code_postal) }}

                    </div>

                    <div class="form-group form-inline">
                        <div class="standard-form-width">
                        {{ form_label(form.ville, 'Ville') }}
                        </div>
                        {{ form_widget(form.ville) }}

                    </div>

                    <div class="form-group form-inline">
                        <div class="standard-form-width">
                        {{ form_label(form.rcs, 'RCS') }}
                        </div>
                        {{ form_widget(form.rcs) }}

                    </div>

                    <div class="form-group form-inline">
                        <div class="standard-form-width">
                        {{ form_label(form.greffe, 'Greffe') }}
                        </div>
                        {{ form_widget(form.greffe) }}

                    </div>

                </div>

                <br/>
                <br/>

                <div class="nouvelle_objet">

                    <div class="form-group">
                        <p>NOUVEL OBJET SOCIAL</p>
                        <hr>
                    </div>

                    <div class="form-group form-inline">
                        <div class="standard-form-width">
                        {{ form_label(form.nouvel_objet_social) }}
                        </div>
                        {{ form_widget(form.nouvel_objet_social) }}

                    </div>

                    <div class="form-group" style="width: 405px;">

                        {{ form_label(form.objet) }}
                        {{ form_widget(form.objet) }}

                    </div>

                </div>

                <br/>
                <br/>

                {% if app.user is null %}

                    <div class="form-group">
                        <p>VOS INFORMATIONS PERSONNELLES <i class="qtip tip-right" data-tip="Si vous avez déjà un compte, veuillez-vous connecter" style="z-index: 2;">
                                <span class="ion-ios-help-outline"></span></i></p>

                        <hr>

                        <div class="form-group form-inline">
                            <div class="standard-form-width">
                            {{ form_label(form_user.nom) }}
                            </div>
                            {{ form_widget(form_user.nom) }}
                        </div>

                        <div class="form-group form-inline">
                            <div class="standard-form-width">
                            {{ form_label(form_user.email) }}
                            </div>
                            {{ form_widget(form_user.email) }}
                        </div>

                        <hr>

                    </div>

                {% endif %}

                <div class="form-group" style="text-align: center">
                    <button class="btn btn-primary" type="submit">Visualiser mon annonce</button>
                </div>
            </form>
        </div>
        {{ form_end(form) }}
    </div>

{% endblock %}

{% block javascripts %}
    {{ parent() }}

    {{ encore_entry_script_tags('chgt_objet_social_creer') }}

{% endblock %}

At last the formtype related to this:

<?php

namespace App\Form;

use App\Entity\ChangementObjetSocial;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ChangementObjetSocialType extends AbstractType
{
   public function buildForm(FormBuilderInterface $builder, array $options)
   {
       $builder
           ->add('en_tete', TextareaType::class, [ 'attr' => ['name' => 'entete',
                                                              'placeholder' => 'Saississez votre en-tête',
                                                              'rows' => '14' ],
                                                              'required' => false])
           ->add('organisme_de_decision', ChoiceType::class, [ 'expanded' => false,
                                                                          'multiple' => false,
                                                                                'choices' => ['l\'AGO' => 'LAGO',
                                                                                'l\'AGE' => 'LAGE',
                                                                                'l\'AGM' => 'LAGM',
                                                                                'Le Gérant' => 'GERANT',
                                                                                'Le Président' => 'PRESIDENT',
                                                                                'L\'Associé Unique' => 'ASSOCIE_UNIQUE',
                                                                                'le CA' => 'LECA']])
           ->add('date_decision', DateType::class, [ 'widget' => 'single_text', 'attr' => ['type' => 'date']])
           ->add('date_d_effet', DateType::class, [ 'widget' => 'single_text', 'attr' => ['type' => 'date', 'class' => 'bg-light'], 'required' => false])
           ->add('denomination', TextType::class,  [ 'attr' => ['onkeyup' => 'this.value = this.value.toUpperCase()',
                                                                'placeholder' => 'Nom dans les statuts, en MAJUSCULE'],
                                                                'label_attr' => ['type' => 'text']])
           ->add('forme', ChoiceType::class, [ 'expanded' => false,
                                                          'multiple' => false,
                                                          'choices' => ['SARL' => 'SARL',
                                                              'SARL à capital variable'=> 'SARL à capital variable',
                                                              'EURL' => 'EURL',
                                                              'EURL à capital variable' => 'EURL à capital variable',
                                                              'SCI' => 'SCI',
                                                              'SCI à capital variable' => 'SCI à capital variable',
                                                              'SA' => 'SA',
                                                              'SAS' => 'SAS',
                                                              'SAS à capital variable' => 'EURL à capital variable',
                                                              'SNC' => 'SNC',
                                                              'SELARL' => 'SELARL',
                                                              'Société Civile' => 'Société Civile',
                                                              'Société Civile de moyens' => 'Société Civile de moyens',
                                                          ]])
           ->add('adresse', TextType::class, [ 'attr' => ['type' => 'text',
                                                          'name' => 'adresse1', 'placeholder' => 'Adresse de la société']])
           ->add('adresse_suite',TextType::class, [ 'attr' => ['type' => 'text', 'name' => 'adresse2',
                                                               'placeholder' => 'Complément d\'adresse', 'class' => 'bg-light'],
                                                               'required' => false])
           ->add('code_postal', TextType::class, [ 'attr' => ['class' => 'form-control', 'type' => 'text',
                                                              'name' => 'Code postal',
                                                                'placeholder' => 'ex: 75001']])
           ->add('ville', TextType::class, [ 'attr' => ['type' => 'text', 'name' => 'ville',
                                                        'placeholder' => 'ex: Paris']])
           ->add('greffe', TextType::class, [ 'attr' => ['type' => 'text', 'name' => 'greffe',
                                                         'placeholder' => 'Greffe concerné']])
           ->add('capital', IntegerType::class, [ 'attr' => ['type' => 'number', 'name' => 'Capital',
                                                             'placeholder' => 'Capital en euro(s)', 'min' => '1']])
           ->add('rcs', TextType::class, [ 'attr' => ['placeholder' => 'RCS tel que dans les statuts'],
                                                      'label_attr' => ['type' => 'text']])
           ->add('nouvel_objet_social', ChoiceType::class,[  'expanded' => false,
                                                                        'multiple' => false,
                                                                        'choices' => [
                                                                            'Extension' => 'Extension',
                                                                            'Modification' => 'Modification'
                                                                        ]])
           ->add('objet', TextareaType::class, [ 'attr' => ['type' => 'text', 'name' => 'Objet',
                                                            'placeholder' => 'Objet tel qu\'il apparait dans les statuts' ]])
       ;
   }

   public function configureOptions(OptionsResolver $resolver)
   {
       $resolver->setDefaults([
           'data_class' => ChangementObjetSocial::class,
       ]);
   }
}
0

There are 0 best solutions below