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

[Validator] Constraint validator "Symfony\Component\Validator\Constraints\RequiredValidator" does not exist or is not enabled #36723

Closed
w1nsun opened this issue May 6, 2020 · 15 comments

Comments

@w1nsun
Copy link

w1nsun commented May 6, 2020

Symfony version(s) affected: 3.4.40

3.4.39 is OK

Description
I get an error: Constraint validator "Symfony\Component\Validator\Constraints\RequiredValidator" does not exist or is not enabled. Check the "validatedBy" method in your constraint class "Symfony\Component\Validator\Constraints\Required".
Line: 49
File: "/data/www/site/current/vendor/symfony/symfony/src/Symfony/Component/Validator/ContainerConstraintValidatorFactory.php"

How to reproduce

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Required;
use Symfony\Component\Validator\Validator\ValidatorInterface;

//$this->validator - ValidatorInterface (service @validator)
$violations = $this->validator->validate($value, new All([
            new Collection([
                'email' => [
                    new NotBlank(),
                    new Email(),
                ],
                'verified' => [
                    new Required(),
                    new Choice(['strict' => true, 'choices' => [1, 0, "1", "0", true, false]]),
                ],
            ]),
        ]));
@aidenko
Copy link

aidenko commented May 6, 2020

I have the same on 4.4.8

@dmaicher
Copy link
Contributor

dmaicher commented May 6, 2020

Cannot reproduce this on 4.4.8

@codegain
Copy link
Contributor

codegain commented May 6, 2020

I have the same on 4.4.8 with Required and Òptional constraint.
No error on 4.4.7.

@dmaicher
Copy link
Contributor

dmaicher commented May 6, 2020

Can anyone provide a small reproducer?

For me this works fine on 4.4.8:

<?php

require_once 'vendor/autoload.php';

use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Required;

$kernel = new AppKernel('dev', true);
$kernel->boot();

$violations = $kernel->getContainer()->get('validator')->validate([[]], new All([
    new Collection([
        'email' => [
            new NotBlank(),
            new Email(),
        ],
        'verified' => [
            new Required(),
            new Choice(['strict' => true, 'choices' => [1, 0, "1", "0", true, false]]),
        ],
    ]),
]));

dump($violations);

@codegain
Copy link
Contributor

codegain commented May 6, 2020

@dmaicher

<?php

require dirname(__DIR__).'/config/bootstrap.php';

use Symfony\Component\Validator\Constraints\Required;

$kernel = new \App\Kernel('dev', true);
$kernel->boot();

$violations = $kernel->getContainer()->get('validator')->validate(null, [
    new Required(),
]);

dump($violations);

In my case this prints for 4.4.8:

Constraint validator "Symfony\Component\Validator\Constraints\RequiredValidator" does not exist or is not enabled. Check the "validatedBy" method in your constraint class "Symfony\Component\Validator\Constraints\Required".

and for 4.4.7:

Symfony\Component\Validator\ConstraintViolationList {#712
  -violations: []
}

@xabbuh
Copy link
Member

xabbuh commented May 7, 2020

The best would be if one of you could provide a small example application allowing to reproduce the issue as suggested by @dmaicher.

@R11baka
Copy link

R11baka commented May 7, 2020

@xabbuh Here reproducer for a bug! Reproducer
Screenshot 2020-05-07 at 13 00 45
Status: reviewed

@aidenko
Copy link

aidenko commented May 7, 2020

In my case the issue was reproduced on 4.4.8

the code created for symfony 3.3.36 and now updated to the 4.4.8. After this update I experienced the issue.

Just for test I downgraded 4.4.8 -> 4.4.7 and could not reproduce the issue.

After that I updated to 4.4.8 again. And everything works and the issue not reproduced

@xabbuh
Copy link
Member

xabbuh commented May 7, 2020

@R11baka Thank you, I was able to reproduce. The behaviour changes is related to #36365. I don't know how we could achieve both (the solved issue and the way the constraint is used in the example). However, using the Required constraint in the way as in the reproducer doesn't make much sense. So I am not sure if we really need to change anything.

@xabbuh
Copy link
Member

xabbuh commented May 7, 2020

/cc @HeahDude

@codegain
Copy link
Contributor

codegain commented May 7, 2020

@xabbuh I'm using the Required constraint in a form in the constraints array. Just modify the controller in @R11baka's reproducer to:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\Constraints\Required;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\HttpFoundation\Request;

class Test extends AbstractController
{

    public function test(Request $request){
        $form = $this->get('form.factory')->createNamedBuilder('testForm')
                ->add('test', HiddenType::class, [ 'constraints' => [ new Required() ] ])
                ->getForm();
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
                dump('valid');
        }

        dump('not submitted');
    }

}

Then require symfony/form via composer, restart the symfony web server, openhttp://127.0.0.1:8000/?testForm[test]=foo and the output is:

Constraint validator &quot;Symfony\Component\Validator\Constraints\RequiredValidator&quot; does not exist or is not enabled. Check the &quot;validatedBy&quot; method in your constraint class &quot;Symfony\Component\Validator\Constraints\Required&quot;. (500 Internal Server Error)

Is this an unsupported use case for constraints?

@xabbuh
Copy link
Member

xabbuh commented May 7, 2020

Basically yes, or to be more precise: Before the constraint placed in this way was just useless (i.e. there was no difference in having it or not). The Required constraint is meant to be used inside a Collection constraint: https://symfony.com/doc/current/reference/constraints/Collection.html#required-and-optional-field-constraints

@stof
Copy link
Member

stof commented May 13, 2020

@codegain I'm quite sure that your use case was actually to use NotBlank, not Required

@codegain
Copy link
Contributor

@stof Currently I use the Optional constraint like this:

'constraints' => [
    (null === $someVariable) ? new NotBlank() : new Optional(),
]

I know there are other mechanisms like validation groups but that seemed to be the easiest option since null is not allowed. It seems I will have to rewrite some of these forms if this is no longer possible.

@xabbuh
Copy link
Member

xabbuh commented May 21, 2020

see #36894

nicolas-grekas added a commit that referenced this issue May 30, 2020
…tional) constraints (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Validator] never directly validate Existence (Required/Optional) constraints

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36637 #36723
| License       | MIT
| Doc PR        |

Using `Optional` or `Required` like "regular" constraints does not make any sense, but doing so didn't break before #36365. I suggest to ignore them for now and deprecate using them outside the `Collection` constraint in 5.2.

Commits
-------

d333aae never directly validate Existence (Required/Optional) constraints
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

9 participants