@UniqueEntity对我不起作用。在控制器中,我使用$ form-> isValid()并通过表单,但不应这样做。取而代之的是我自己定义的消息,我得到了MySQL错误:
SQLSTATE [23000]:违反完整性约束:1062键“ UNIQ_115E494BF47645AE”的条目“ media30”重复
我有以下几种形式:
class BIPType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
$builder->add('url');
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'AppBundle\Entity\Bip',
);
}
}
接下来是用于注册的覆盖FOSUser UserType表单,其中包括BIPType:
class UserType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$biptype = new BIPType();
$builder
->add('bip', $biptype, array(
'data_class'=>'AppBundle\Entity\Bip'))
->add('nazwisko')
->add('imie')
;
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
// Or for Symfony < 2.8
// return 'fos_user_registration';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
// For Symfony 2.x
public function getName()
{
return $this->getBlockPrefix();
}
}
和BIP实体
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity
* @UniqueEntity(fields="url", message="URL is already in use")
* @ORM\Table(name="bips")
*/
class Bip
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="url", type="string", unique=true)
*/
protected $url;
/**
* @ORM\Column(type="string")
* @Assert\Length(
* min = "3",
* max = "25",
* minMessage = "Nazwa BIPu musi mieć conajmniej 3 znaki.",
* maxMessage = "Nazwa BIPu może mieć conajwyżej 25 znaki."
*)
*/
protected $name;
/**
* @Assert\Image(
* minWidth = 80,
* maxWidth = 200,
* minHeight = 80,
* maxHeight = 200
* )
*/
protected $file;
/**
* Sets file.
*
* @param UploadedFile $file
*/
private $temp;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $path;
/**
* @ORM\Column(type="boolean", nullable=false)
*/
private $public;
最佳答案
如果要使用fos_user.registration.form.factory
的FOSUserBundle
服务创建表单,则默认情况下,在验证表单时将使用Registration
验证组。将名为Registration
的验证组添加到您的name属性。
/**
* @ORM\Entity
* @ORM\Table(name="bips")
* @UniqueEntity(fields="url", message="URL is already in use", groups={"Registration", "Profile"})
*/
class Bip
{
然后加
use Symfony\Component\OptionsResolver\OptionsResolver;
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => array('Registration'),
));
}
到您的
BIPType
类。关于symfony - UniqueEntity不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36221481/