Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation failed on required fields check #9

Open
Digi92 opened this issue Apr 26, 2024 · 0 comments
Open

Validation failed on required fields check #9

Digi92 opened this issue Apr 26, 2024 · 0 comments

Comments

@Digi92
Copy link

Digi92 commented Apr 26, 2024

Hi,
in a freshly installed Symfony 6.4 the valiadation in mosparo-bundle/src/Validator/IsValidMosparoValidator.php:90 fails.
In the Mosparo result request and later in “$verifiedFields” the fields are written as “contact[firstname]”.
From “mosparo-bundle/src/Validator/IsValidMosparoValidator.php:77->$this->normalizer->normalize($form);” and later “$requiredFields” the fields are returned as “contactfirstname”.
It seems that there is a mismatch between the two values.

To recreate:

  1. Run the command: composer create-project symfony/skeleton:^6.4 htdocs && cd htdocs && composer require webapp && composer require arnaud-ritti/mosparo-bundle
  2. Setup this bundle: https://github.com/arnaud-ritti/mosparo-bundle?tab=readme-ov-file#configuration
  3. Add my simple form code:
    src/Controller/ContactFormController.php

namespace App\Controller;

use App\Form\Type\ContactType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class ContactFormController extends AbstractController
{
    #[Route('/contact', name: 'contact_form')]
    public function new(Request $request): Response
    {
        $form = $this->createForm(ContactType::class);
        
        $form->handleRequest($request);
        if ($form->isSubmitted() &&
            $form->isValid()
        ) {
            $contact = $form->getData();
            
            return $this->redirectToRoute('contact_form_success');
        }
        
        return $this->render('contact/new.html.twig', [
            'form' => $form,
        ]);
    }

    #[Route('/success', name: 'contact_form_success')]
    public function success(): Response
    {
        return $this->render('contact/success.html.twig', []);
    }
}

src/Entity/Contact.php

<?php

namespace App\Entity;

class Contact
{
    #[Assert\NotBlank]
    protected string $firstname;

    #[Assert\NotBlank]
    protected string $lastname;

    #[Assert\NotBlank]
    protected string $mail;

    #[Assert\NotBlank]
    protected string $message;

    public function getFirstname(): string
    {
        return $this->firstname;
    }

    public function setFirstname(string $firstname): void
    {
        $this->firstname = $firstname;
    }

    public function getLastname(): string
    {
        return $this->lastname;
    }

    public function setLastname(string $lastname): void
    {
        $this->lastname = $lastname;
    }

    public function getMail(): string
    {
        return $this->mail;
    }

    public function setMail(string $mail): void
    {
        $this->mail = $mail;
    }

    public function getMessage(): string
    {
        return $this->message;
    }

    public function setMessage(string $message): void
    {
        $this->message = $message;
    }
}

src/Form/Type/ContactType.php

<?php

namespace App\Form\Type;

use Mosparo\MosparoBundle\Form\Type\MosparoType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use App\Entity\Contact;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ContactType extends AbstractType
{
    #[\Override]
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('firstname', TextType::class, ['required' => true])
            ->add('lastname', TextType::class, ['required' => true])
            ->add('mail', TextType::class, ['required' => true])
            ->add('message', TextareaType::class, ['required' => true])
            ->add('mosparo', MosparoType::class, [
                'project' => 'default',
                'allowBrowserValidation' => false,
                'cssResourceUrl' => '',
                'designMode' => false,
                'inputFieldSelector' => '[name]:not(.mosparo__ignored-field)',
                'loadCssResource' => true,
                'requestSubmitTokenOnInit' => true,
            ])
            ->add('send', SubmitType::class, ['label' => 'Send'])
        ;
    }

    #[\Override]
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Contact::class,
        ]);
    }
}

templates/contact/new.html.twig

{{ form(form) }}

templates/contact/success.html.twig

<h1>Success</h1>

public/.htaccess

<IfModule mod_rewrite.c>
  RewriteEngine On

  # we skip all files with .something
  RewriteCond %{REQUEST_URI} \..+$
  RewriteCond %{REQUEST_URI} !\.html$
  RewriteRule .* - [L]

  # we check if the .html version is here (caching)
  RewriteRule ^$ index.html [QSA]
  RewriteRule ^([^.]+)$ $1.html [QSA]
  RewriteCond %{REQUEST_FILENAME} !-f

  # no, so we redirect to our front web controller
  RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
  1. Try to send the validated form
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant