Symfony2 UniqueEntity约束SQL错误而不是消息

时间:2015-08-24 19:00:38

标签: php validation symfony unique

我坚持用Symfony2创建用户注册表单。 我正在尝试在email类的User属性上定义唯一约束。

的Acme \的appbundle \实体\ user.php的

namespace Acme\APPBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
* @ORM\Entity
* @ORM\Entity(repositoryClass="Acme\APPBundle\Entity\UserRepository")
* @ORM\Table("users")
* @UniqueEntity(
*       fields={"email"},
*       message="email already used"
* )
*/
class User implements UserInterface, \Serializable
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     * @Assert\NotBlank()
     * @Assert\Email()
     */
    protected $email;


    [...]
}

的Acme \的appbundle \表格\型号\ UserType.php

namespace Acme\APPBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('email', 'email');
        $builder->add('password', 'repeated', array(
            'first_name' => 'password',
            'second_name' => 'confirm',
            'type' => 'password',
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\APPBundle\Entity\User',
            'cascade_validation' => true,
        ));
    }

    public function getName()
    {
        return 'user';
    }
}

我在文档后面添加了约束,但我仍然得到一个例外:

An exception occured while executing 'INSERT INTO users ( ... )'
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

看起来注释中定义的message值被忽略,并且表单验证被绕过,因为它在尝试在数据库中插入行之前会失败。

你能告诉我我做错了什么吗?

编辑:

按照Matteo'Ingannatore'G。的建议,我注意到我的表单没有经过适当的验证。 我忘了提到我使用扩展用户表单的注册类。我按照Symfony Cookbook中解释的内容编写了我的代码。

因此我有:

的Acme \的appbundle \表格\模型\为registration.php

namespace Acme\APPBundle\Form\Model;

use Symfony\Component\Validator\Constraints as Assert;

use Acme\APPBundle\Entity\User;

class Registration
{
    /**
     * @Assert\Type(type="Acme\APPBundle\Entity\User")
     */
    protected $user;

    /**
     * @Assert\NotBlank()
     * @Assert\True()
     */
    protected $termsAccepted;

    public function setUser(User $user)
    {
        $this->user = $user;
    }

    public function getUser()
    {
        return $this->user;
    }

    public function getTermsAccepted()
    {
        return $this->termsAccepted;
    }

    public function setTermsAccepted($termsAccepted)
    {
        $this->termsAccepted = (Boolean) $termsAccepted;
    }
}

的Acme \的appbundle \表格\型号\ RegistrationType.php

namespace Acme\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());
        $builder->add('terms', 'checkbox', array('property_path' => 'termsAccepted'));
    }

    public function getName()
    {
        return 'registration';
    }
}

的Acme \的appbundle \控制器\ AccountController.php

namespace Acme\APPBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

use Acme\AccountBundle\Form\Type\RegistrationType;
use Acme\AccountBundle\Form\Model\Registration;

class AccountController extends Controller
{
    public function registerAction()
    {
        $form = $this->createForm(new RegistrationType(), new Registration());

        return $this->render('AcmeAPPBundle:Account:register.html.twig', array('form' => $form->createView()));
    }



    public function createAction()
    {
        $em = $this->getDoctrine()->getEntityManager();

        $form = $this->createForm(new RegistrationType(), new Registration());

        $form->handleRequest($this->getRequest());

        if ($form->isValid()) { // FIXME !!
            $registration = $form->getData();

            $em->persist($registration->getUser());
            $em->flush();

            return $this->redirect($this->generateUrl('home'));
        }

        return $this->render('AcmeAPPBundle:Account:register.html.twig', array('form' => $form->createView()));
    }

}

我想我得到的错误可能是由于Registration表单已经过验证,但其中的User表单未提交验证。我错了吗 ? 我怎样才能简单地改变这种行为?我看到有一个cascade_validation选项,但在这里似乎没用。 我认为Symfony Cookbook提供创建用户提供者和创建注册表单的指南很奇怪,但没有解释如何让这些工作顺利进行。

0 个答案:

没有答案