如果by_reference为false / on true,则禁用symfony2验证

时间:2014-03-19 12:00:06

标签: validation symfony types doctrine-orm symfony-forms

我不明白这是什么问题" by_reference"选项...

问题在于" form-> isValid()"说"是"但电子邮件收集有效。 如果我将by_reference设置为true,则表单表示" form-> isValid()"是假的。

但是为什么它不起作用by_reference =>假??

我的用户表单。

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('emails', 'collection', array(
                'type' => new UserEmailType(),
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false // if true it works!
            ))
        ;
    }

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

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

我的用户电子邮件表单。

class UserEmailType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('email', 'email')
            ->add('comment', 'text', array('required' => false))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\DemoBundle\Entity\UserEmail'
        ));
    }

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

用户实体。

/**
 * @ORM\Table(name="user")
 * @ORM\Entity()
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="UserEmail", mappedBy="user", cascade={"all"}, orphanRemoval=true)
     * Following doesnt works :D
     * @Assert\Valid
     */
    private $emails;

    public function __construct()
    {
        $this->emails = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function getId()
    {
        return $this->id;
    }

    public function addEmail(\Acme\DemoBundle\Entity\UserEmail $email)
    {
        $email->setUser($this);
        $this->emails[] = $email;

        return $this;
    }

    public function removeEmail(\Acme\DemoBundle\Entity\UserEmail $email)
    {
        $email->setUser();
        $this->emails->removeElement($email);
    }

    public function getEmails()
    {
        return $this->emails;
    }
}

用户电子邮件实体。

/**
 * @ORM\Table(name="user_email", uniqueConstraints={@ORM\UniqueConstraint(name="email_idx", columns={"email", "user"})})
 * @ORM\Entity()
 * @UniqueEntity({"email", "user"})
 */
class UserEmail
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(name="email", type="string", length=255)
     * @Assert\Email(checkMX = false)
     */
    private $email;

    /**
     * @ORM\Column(name="comment", type="string", nullable=true)
     */
    private $comment;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="emails")
     * @ORM\JoinColumn(name="user", referencedColumnName="id", onDelete="CASCADE")
     * @Assert\NotNull()
     */
    private $user;

    /**
        ID
    */

    public function getId()
    {
        return $this->id;
    }

    /**
        Email
    */

    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    public function getEmail()
    {
        return $this->email;
    }

    /**
        Comment
    */

    public function setComment($comment)
    {
        $this->comment = $comment;

        return $this;
    }

    public function getComment()
    {
        return $this->comment;
    }

    /**
        User
    */

    public function setUser(\Acme\DemoBundle\Entity\User $user = null)
    {
        $this->user = $user;

        return $this;
    }

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

如果我使用print_r($form->getErrorsAsString());die();调试表单,我就明白了。

ERROR: This value is not a valid email address.
emails:
    0:
        email:
            No errors
        comment:
            No errors
    1:
        email:
            No errors
        comment:
            No errors

我删除了未使用的评论等 数据库已与当前实体元数据同步。 并且导入了断言注释。

提前致谢! :)

0 个答案:

没有答案