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 groups and form without entity #31990

Closed
MPierre68 opened this issue Jun 11, 2019 · 3 comments
Closed

Validation groups and form without entity #31990

MPierre68 opened this issue Jun 11, 2019 · 3 comments

Comments

@MPierre68
Copy link

Symfony version(s) affected: 3.4.24 to 3.4.28

Hello, I'm working with form without underling entity and I need to use validation groups.
So i use this doc https://symfony.com/doc/3.4/validation/raw_values.html to affect validation to differents fields
Unfortunatly, only the first group specifed is used by the validator. The others are ignored.

Here is a form type :

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class DemoType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('field1', TextType::class, [
		'required' => false,
	    ])
	    ->add('field2', TextType::class, [
		'required' => false,
 	    ])
	    ->add('field3', TextType::class, [
		'required' => false,
	    ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'validation_groups' => ['field1', 'field2', 'field3']
        ]);
    }
}

And there a controller :

namespace App\Controller;

use App\Form\DemoType;
use App\Form\Demo2Type;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints as Assert;

class DemoController extends Controller
{
    /**
     * @Route("/", name="demo")
     */
    public function index(Request $request)
    {
	$contraintes = new Assert\Collection([
		'field1' => new Assert\NotBlank([
			'groups' => ['field1']
		]),
		'field2' => new Assert\NotBlank([
			'groups' => ['field2']
		]),
		'field3' => new Assert\NotBlank([
			'groups' => ['field3']
		]),
	]);
		
	$form = $this->createForm(DemoType::class, null, ['constraints' => $contraintes]);
		
	$form->handleRequest($request);
		
	if ($form->isSubmitted()) {
            if ($form->isValid()) {
		$this->get('session')->getFlashBag()->add('success', 'Formulaire soumis avec succès.');
	    } else {
		$this->get('session')->getFlashBag()->add('error', 'Les données saisies sont invalides.');
	    }
	}
		
        return $this->render('demo/index.html.twig', [
            'controller_name' => 'DemoController',
		'form' => $form->createView(),
        ]);
    }
}

to reproduce the issue.

@xabbuh
Copy link
Member

xabbuh commented Jun 14, 2019

Status: Reviewed

@xabbuh
Copy link
Member

xabbuh commented Jun 14, 2019

please try the fix I suggest in #32037

@fabpot fabpot closed this as completed Jun 14, 2019
fabpot added a commit that referenced this issue Jun 14, 2019
This PR was merged into the 3.4 branch.

Discussion
----------

[Form] validate composite constraints in all groups

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #31990
| License       | MIT
| Doc PR        |

Commits
-------

94ded00 validate composite constraints in all groups
@MPierre68
Copy link
Author

It fixed for me. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants