fkoessler
fkoessler

Reputation: 7257

validating fosuserbundle registration form

I am using FOSUserBundle. In my project, I created my own UserBundle and overrode the controller, the forms and the handlers. Now when a user tries to register with an existing email, the site crashes (email is unique for doctrine). It seems like there is no validation made. I thought there would be some validation as I have in my validation.yml:

YOP\UserBundle\Entity\User:
  constraints:
    - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: email
  properties:
    email:
      - Email: ~

How come the validation isn't made on the email field? How can I make sure that my validation constraints are taken into account?

PS: the validation.yml file is not in my UserBundle, is that a problem?

EDIT:

the code of my UserBundle is available here I don't understand why there is no validation done anymore...

Upvotes: 1

Views: 5755

Answers (2)

fkoessler
fkoessler

Reputation: 7257

Solved the issue by:

  • having the validation.yml file in the same bundle as the one containing my User entity, and pointing to my User Entity
  • adding the "Registration" validation group to my fields

validation.yml:

YOP\YourOwnPoetBundle\Entity\User:
  constraints:
    - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: email
  properties:
    email:
      - Email: { groups: [Registration] }
    plainPassword:
      - MinLength: { limit: 5, message: "Your password must have at least {{ limit }} characters" }
    name:
      - NotBlank: { groups: [Registration] }
    familyName:
      - NotBlank: { groups: [Registration] }
    sex:
      - Choice: { choices: [1, 2], groups: [Registration] }
    birthdate:
      - NotNull: { groups: [Registration] }
    city:
      - NotBlank: { groups: [Registration] }
    howDidYouHear:
      - Choice: { choices: [1, 2, 3, 4, 5, 6, 7, 8], groups: [Registration] }

Upvotes: 6

poojitha
poojitha

Reputation: 335

validation.yml should be in your user bundle.
/app/config/config.yml should be updated as follow:

validation:      { enabled: true,  enable_annotations: false }

Upvotes: -1

Related Questions