I have a Doctrine user entity that I am trying to add form validators to for the registration form but they do not fire for the registration form under any condition.
My user entity:
namespace JMSHockey\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* My\AppBundle\Entity\User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="My\AppBundle\Entity\UserRepository")
* @UniqueEntity(fields={"email"}, message="Sorry, this email address is already in use.", groups={"registration"})
* @UniqueEntity(fields={"username"}, message="Sorry, this username is already taken.", groups={"registration"})
*/
class User implements AdvancedUserInterface,\Serializable
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $email
*
* @ORM\Column(name="email", type="string", length=75, nullable=false)
* @Assert\Email()
* @Assert\NotBlank()
*/
private $email;
/**
* @var string $username
*
* @ORM\Column(name="username", type="string", length=75, nullable=false)
* @Assert\NotBlank()
*/
private $username;
...
}
Here's my UserType form:
namespace My\AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('email','email');
$builder->add('username','text');
...
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'My\AppBundle\Entity\User',
'validation_groups' => array('registration'),
);
}
public function getName()
{
return 'user';
}
}
And lastly, the registration form:
namespace My\AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('user', new UserType());
}
public function getName()
{
return 'registration';
}
}
It seems like I must be missing something obvious here, but between the Symfony manual and the resources I've found online, I haven't been able to determine what it is.
I had the same problem.
I resolved it by added cascade_validation
at true in the setDefaultOptions
method in your RegistrationType
class and used the OptionsResolverInterface
class from the OptionsResolver Symfony component:
namespace My\AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('user', new UserType());
}
public function getName()
{
return 'registration';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'cascade_validation' => true
));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With