我有一個Doctrine用戶實體,我試圖將表單驗證器添加到註冊表單中,但它們在任何情況下都不會觸發註冊表單。驗證約束不會觸發註冊表單
我的用戶實體:
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;
...
}
這裏是我的用戶等級和積分形式:
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';
}
}
最後,報名表:
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';
}
}
好像我必須失去了一些東西明顯在這裏,但是在Symfony手冊和我在網上找到的資源之間,我還沒有弄清楚呃,它是什麼。
你有[啓用](http://symfony.com /doc/current/book/validation.html#configuration)批註進行驗證? – meze
是的,我在框架部分的config.yml中設置了'validation:{enable_annotations:true}'。 –
您是否嘗試過在您的RegistrationType上專門設置cascade_validation? – m0c