Symfony2忽略了收集的形式验证



我对symfony2和电子邮件地址集合的验证有问题。

ENTITY USER

/**
 * User
 *
 * @ORMTable()
 * @ORMEntity
 */
class User
{
    /**
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @var Email[]
     * @ORMOneToMany(targetEntity="Email", mappedBy="user", cascade={"all"}, orphanRemoval=true)
     * @AssertValid
     */
    private $emails;
    public function __construct()
    {
        $this->emails = new DoctrineCommonCollectionsArrayCollection();
    }
    public function getId()
    {
        return $this->id;
    }
    /**
     * Add emails
     *
     * @param Email $emails
     * @return User
     */
    public function addEmail(Email $emails)
    {
        if($emails->getEmail() !== null && strlen($emails->getEmail()) > 0)
        {
            $emails->setUser($this);
            $this->emails[] = $emails;
        }
        return $this;
    }
    /**
     * Remove emails
     *
     * @param Email $emails
     */
    public function removeEmail(Email $emails)
    {
        $emails->setUser();
        $this->emails->removeElement($emails);
    }
    /**
     * Set emails
     *
     * @param Email[] $emails
     * @return User
     */
    public function setEmails($emails)
    {
        $this->emails = array();
        foreach($emails as $email)
        {
            $this->addEmail($email);
        }
        return $this;
    }
    /**
     * Get emails
     *
     * @return Email[]
     */
    public function getEmails()
    {
        return $this->emails;
    }
}

实体 emaim

/**
 * Email
 *
 * @ORMTable()
 * @ORMEntity
 */
class Email
{
    /**
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @ORMColumn(name="email", type="string", length=255)
     * @AssertEmail(checkMX = false)
     */
    private $email;
    /**
     * @var User
     * @ORMManyToOne(targetEntity="User", inversedBy="emails")
     * @ORMJoinColumn(name="user", referencedColumnName="id", onDelete="CASCADE")
     * @AssertNotNull()
     */
    private $user;
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set email
     *
     * @param string $email
     * @return Email
     */
    public function setEmail($email)
    {
        $this->email = $email;
        return $this;
    }
    /**
     * Get email
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }
    /**
     * Set user
     *
     * @param User $user
     * @return Email
     */
    public function setUser(User $user = null)
    {
        $this->user = $user;
        return $this;
    }
    /**
     * Get user
     *
     * @return User
     */
    public function getUser()
    {
        return $this->user;
    }
}

USERTYPE

/**
 * Class UserType
 */
class UserType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('emails', 'collection', array(
                'type' => new EmailType(),
                'allow_add' => true,
                'allow_delete' => true
            ))
        ;
    }
    public function getName()
    {
        return 'user';
    }
}

emailType

/**
 * Class EmailType
 */
class EmailType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('email', 'email')
        ;
    }
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'EntityUser'
        ));
    }
    public function getName()
    {
        return 'email';
    }
}

所有实体都包含声明注释。

问题是我可以在没有有效的电子邮件地址的情况下保存表单。

  • 123@example.org< - 多数民众赞成在正确
  • 123< - 多数民众赞成在保存< - 多数民众赞成不是正确/没有有效的电子邮件地址!
  • An exception occurred while executing 'INSERT INTO email (email, user) VALUES (?, ?, ?)' with params [null, null]< - 如果我使用" by_reference" = true,我会遇到此错误,如果" by_reference"为false,我没有错误...

我希望您可以帮助我:)

从您的终端运行此命令:

php app/console doctrine:schema:update --force

相关内容

  • 没有找到相关文章