Validation 如果by_引用为false/on true,则symfony2验证将被禁用,它将正常工作
我不明白这个参考选项有什么问题 问题是表单->isValid说是,但电子邮件收集无效。 如果我将by_引用设置为true,则表示form->isValid的表单为false 但是为什么它不能通过_reference=>false工作呢 我的用户表单Validation 如果by_引用为false/on true,则symfony2验证将被禁用,它将正常工作,validation,symfony,types,doctrine-orm,symfony-forms,Validation,Symfony,Types,Doctrine Orm,Symfony Forms,我不明白这个参考选项有什么问题 问题是表单->isValid说是,但电子邮件收集无效。 如果我将by_引用设置为true,则表示form->isValid的表单为false 但是为什么它不能通过_reference=>false工作呢 我的用户表单 class UserType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) {
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调试表单;死亡我知道了
ERROR: This value is not a valid email address.
emails:
0:
email:
No errors
comment:
No errors
1:
email:
No errors
comment:
No errors
我删除了未使用的评论等。
数据库已与当前实体元数据同步。
并导入断言注释
提前谢谢!: 我的朋友,你解决了你的问题吗?没有。我创建了一个新的git分支,然后走了另一条路。我想你的代码的目标和我的差不多。那么,看看这个链接