0
votes

I am using symfony form for validating login data so I tie it to the entity class (which has validation defined for all members) and I need the form to only validate the email and password portion. So in the form class, I don't add the email and password to the form. However, when the data is submitted, it still validates them and shows the errors at the top of the form

How do I get it to not validate the other members(city, sex, etc) without changing the entity class.

So there's my entity class with : name, email, sex, password, city. All fields required

Login form with email and password. I still get errors for the other two

1

1 Answers

2
votes

Use validation groups ...

Entity class :

// src/Acme/BlogBundle/Entity/User.php
namespace Acme\BlogBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;

class User implements UserInterface
{
    /**
    * @Assert\Email(groups={"registration"})
    */
    private $email;

    /**
    * @Assert\NotBlank(groups={"registration"})
    * @Assert\Length(min=7, groups={"registration"})
    */
    private $password;

    /**
    * @Assert\Length(min = "2")
    */
    private $city;
}

Then when you create your form :

$form = $this->createFormBuilder($users, array(
    'validation_groups' => array('registration'),
))->add(...);

This would then only validate the email and password fields.

Docs on validation groups are here and here