Symfony2: How to use validators of a different Bundle
-
08-12-2019 - |
Pergunta
I'm trying to use the validators of FOSUserBundle in my own form, but I don't know how to set up the validator.yml. It currently looks like this:
Acme\UserBundle\Entity\User:
constraints:
- FOS\UserBundle\Validator\Unique:
property: usernameCanonical
- FOS\UserBundle\Validator\Unique:
property: emailCanonical
properties:
username:
- NotBlank: ~
email:
- Email: ~
- NotBlank: ~
mandant:
- NotBlank: ~
- Type: Acme\UserBundle\Entity\Mandant
But this results in the following PHP error:
[05-Mar-2012 14:47:56 UTC] PHP Stack trace:
[05-Mar-2012 14:47:56 UTC] PHP 1. {main}() W:\redacted\symfony_webfrontend\app\console:0
[05-Mar-2012 14:48:10 UTC] PHP Fatal error: Class 'FOS\UserBundle\ValidatorUnique' not found in W:\redacted\symfony_webfrontend\vendor\symfony\src\Symfony\Component\Validator\Mapping\Loader\FileLoader.php on line 73
It looks like the class path is wrong but I can't find any documentation on how to use validators of a different bundle in my own validators.yml...
Solução
Not sure why it actually works now because I tried this before, but this seems the way to go:
namespaces:
FOSUserBundle: FOS\UserBundle\Validator\
Acme\UserBundle\Entity\User:
constraints:
- "FOSUserBundle:Unique":
property: usernameCanonical
- "FOSUserBundle:Unique":
property: emailCanonical
properties:
username:
- NotBlank: ~
email:
- Email: ~
- NotBlank: ~
Mandant:
- NotBlank: ~
- Type: Acme\UserBundle\Entity\Mandant
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow