Symfony2 多对多关系的实体形式问题
我实际上正在学习 Symfony2 并且遇到了问题。电影和演员之间存在多对多的关系。
我创建了一个表单来制作一部新电影。在此表单中,我有一个选定的输入来选择演员,但是当我选择许多演员时,表单会生成错误“此值不应为空”。当我删除演员类中的断言时,我没有这个错误。 为什么我的表单检查演员类中的断言?
/*
* MOVIE CLASS
*
*/
class Movie
{
//...
/**
* @ORM\ManyToMany(targetEntity="Actor", inversedBy="movies")
* @ORM\JoinTable(name="movie_actor")
*/
protected $actors;
}
/*
* ACTOR CLASS
*
*/
class Actor
{
/**
* @ORM\Column(type="string", length="255")
* @Assert\NotBlank()
* @Assert\MinLength(3)
*/
protected $firstName;
/**
* @ORM\Column(type="string", length="255")
* @Assert\NotBlank()
* @Assert\MinLength(3)
*/
protected $lastName;
/**
* @ORM\Column(type="date", nullable = true)
*/
protected $birthday;
/**
* @ORM\Column(type="boolean")
* @Assert\NotBlank()
*/
protected $sexe;
/**
* @ORM\ManyToMany(targetEntity="Movie", mappedBy="actors")
* @Assert\NotNull()
*/
protected $movies;
}
/**
*
* MOVIE FORM
*/
class MovieType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('title')
->add('description')
->add('category')
->add('actors', 'entity',
array(
'class' => 'MyAppFilmothequeBundle:Actor',
'query_builder' => function(EntityRepository $er)
{
return $er->createQueryBuilder('a')->orderBy('a.firstName', 'ASC');
},
'multiple' => true
))
->add('user')
;
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'MyApp\FilmothequeBundle\Entity\Movie');
}
public function getName()
{
return 'movie';
}
}
I’m actually learning Symfony2 and I have a problem. I have a relation many to many between a movie and an actor.
I have created a form to create a new movie. In this form I have a selected input to choose actors but when I select many actors the form generated an error “This value should not be blank”. When I delete my assert in actor class I haven’t this error.
Why my form check the assert in my actor class?
/*
* MOVIE CLASS
*
*/
class Movie
{
//...
/**
* @ORM\ManyToMany(targetEntity="Actor", inversedBy="movies")
* @ORM\JoinTable(name="movie_actor")
*/
protected $actors;
}
/*
* ACTOR CLASS
*
*/
class Actor
{
/**
* @ORM\Column(type="string", length="255")
* @Assert\NotBlank()
* @Assert\MinLength(3)
*/
protected $firstName;
/**
* @ORM\Column(type="string", length="255")
* @Assert\NotBlank()
* @Assert\MinLength(3)
*/
protected $lastName;
/**
* @ORM\Column(type="date", nullable = true)
*/
protected $birthday;
/**
* @ORM\Column(type="boolean")
* @Assert\NotBlank()
*/
protected $sexe;
/**
* @ORM\ManyToMany(targetEntity="Movie", mappedBy="actors")
* @Assert\NotNull()
*/
protected $movies;
}
/**
*
* MOVIE FORM
*/
class MovieType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('title')
->add('description')
->add('category')
->add('actors', 'entity',
array(
'class' => 'MyAppFilmothequeBundle:Actor',
'query_builder' => function(EntityRepository $er)
{
return $er->createQueryBuilder('a')->orderBy('a.firstName', 'ASC');
},
'multiple' => true
))
->add('user')
;
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'MyApp\FilmothequeBundle\Entity\Movie');
}
public function getName()
{
return 'movie';
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
验证是通过向类添加一组规则(称为约束)来完成的。因此,您的表单会查看您放入相关类中的断言,以检查提交的数据是否有效。查看表单一章。
还有一件事:实体字段作为“必需”选项 默认设置为“true”。如果你确实想要的话,只需将其设置为 false 即可。
Validation is done by adding a set of rules (called constraints) to a class. So your form looks at assertions you put in the related class to check if submitted data is valid or not. Check the Forms chapter.
One more thing: entity field as a "required" option set to "true" by default. Just set it to false, if you really want it.