Skip to content

[Form][Validator] Validation not working if not mapped #20880

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

Closed
GrinWay opened this issue Apr 13, 2025 · 7 comments
Closed

[Form][Validator] Validation not working if not mapped #20880

GrinWay opened this issue Apr 13, 2025 · 7 comments

Comments

@GrinWay
Copy link
Contributor

GrinWay commented Apr 13, 2025

Even if a checkbox is not checked form will pass validation (no errors), hmm... strange

To reproduce

$builder->add('agreeToTerms', CheckboxType::class, [
    // validation doesn't work if a field is not mapped
    'mapped' => false,
    'constraints' => [
        new Assert\IsTrue(),
    ],
]);

PHP 8.4.2 (cli) (built: Dec 17 2024 17:30:12) (ZTS Visual C++ 2022 x64)
Copyright (c) The PHP Group
Zend Engine v4.4.2, Copyright (c) Zend Technologies
with Zend OPcache v8.4.2, Copyright (c), by Zend Technologies

SYMFONY v7.2.5

@94noni
Copy link
Contributor

94noni commented Apr 24, 2025

How is your form handled and validated on controler side ?
Seems very weird

@GrinWay
Copy link
Contributor Author

GrinWay commented Apr 24, 2025

Oh... I have to explain:

Short answer: Everything is fine, validation is not broken

  1. I'm using validation group provider for the entity
  2. I thought: I'm using validation group provider and it gives me (the "1" for instance) group and I was using Assert\IsTrue assertion with that group in my form constraints option, but it's not applied because to make it work I had to pass "validation_groups" option to the form options but I didn't
  3. But when I was using the exact assertion with ("1" group) inside my entity of course I got a validation error

The key point is:
If you want to use form validation you have to use "validation_groups" option instead of relying on a validation group provider that really work for entities only

OMG I even created a new symfony project to understand that, it took me 1 hour...

@GrinWay GrinWay closed this as completed Apr 25, 2025
@94noni
Copy link
Contributor

94noni commented Apr 25, 2025

Perhaps try to open a documentation PR if you think this may be valuable

@GrinWay
Copy link
Contributor Author

GrinWay commented Apr 25, 2025

I should do this

@GrinWay
Copy link
Contributor Author

GrinWay commented Apr 25, 2025

I'll think how to do it better

  1. Maybe an immediate commit in the docs (pull request) to change the validation group provider to say it doesn't influence the form validation

  2. Or suggest to take into account groups supplied by provider when it comes to forms but "validation_groups" will just have a higher priority?

What's better?

@94noni
Copy link
Contributor

94noni commented Apr 25, 2025

I am not so familiar with form but open a PR and I am sure you will get reviews :)

@GrinWay
Copy link
Contributor Author

GrinWay commented Apr 25, 2025

Ok, at any rate thanks a lot

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

4 participants