在具有集合字段的表单中,其中集合包含实体字段(成员类型中的 eid),添加集合验证会导致实体字段引发以下错误:
在渲染模板期间抛出异常(“注意:类 Mana\ClientBundle\Entity\Ethnicity 的对象无法转换为 int
如果没有验证,实体字段会被正确处理。并且将实体字段添加到验证器没有任何效果。
收藏:
->add('members', 'collection', array(
'type' => new MemberType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true,
))
会员类型:
->add('include', 'choice', array(
'choices' => array('Yes' => 'Yes', 'No' => 'No'),
'empty_value' => false,
'required' => false))
->add('fname', null, array('required' => false))
->add('sname', null, array('required' => false))
->add('dob', 'dob_age')
->add('gender', 'choice', array(
'choices' => array('Male' => 'Male', 'Female' => 'Female'),
'empty_value' => false,
'required' => false))
->add('eid', 'entity', array(
'property' => 'abbr',
'label' => '',
'class' => 'ManaClientBundle:Ethnicity',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('e')
->orderBy('e.abbr', 'ASC');
}
));
验证:
/**
* @Assert\Collection(
* fields = {
* "fname" = @Assert\NotBlank(message = "Member first name may not be blank"),
* "dob" = @Assert\NotBlank(message = "Member DOB may not be blank"),
* },
* allowExtraFields = true
* )
*/
会员单位:
class Member
{
/**
* @var integer $id
*
* @ORM\Column(name="hid", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Client",inversedBy="members",cascade={"remove", "persist"})
* @ORM\JoinColumn(name="clientId", referencedColumnName="id")
*
*/
protected $client;
public function setClient(Client $client)
{
$this->client = $client;
return $this;
}
public function getClient()
{
return $this->client;
}
/**
* @var string $fname
*
* @ORM\Column(name="fname", type="string", length=30, nullable=false)
*/
private $fname;
/**
* @var string $sname
*
* @ORM\Column(name="sname", type="string", length=45, nullable=false)
*/
private $sname;
/**
* @var \DateTime $dob
*
* @ORM\Column(name="dob", type="date", nullable=true)
*/
private $dob;
/**
* @var string $include
*
* @ORM\Column(name="include", type="string", nullable=false)
*/
private $include;
/**
* @var string $gender
*
* @ORM\Column(name="gender", type="string", nullable=false)
*/
private $gender;
/**
* @var \DateTime $excludeDate
*
* @ORM\Column(name="exclude_date", type="date", nullable=true)
*/
private $excludeDate;
/**
* @var integer $eid
*
* @ORM\Column(name="eid", type="integer", nullable=true)
*/
private $eid;
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* Set fname
*
* @param string $fname
* @return member
*/
public function setFname($fname) {
$this->fname = $fname;
return $this;
}
/**
* Get fname
*
* @return string
*/
public function getFname() {
return $this->fname;
}
/**
* Set sname
*
* @param string $sname
* @return member
*/
public function setSname($sname) {
$this->sname = $sname;
return $this;
}
/**
* Get sname
*
* @return string
*/
public function getSname() {
return $this->sname;
}
/**
* Set dob
*
* @param \DateTime $dob
* @return member
*/
public function setDob($dob) {
$this->dob = $dob;
return $this;
}
/**
* Get dob
*
* @return \DateTime
*/
public function getDob() {
return $this->dob;
}
/**
* Set include
*
* @param string $include
* @return member
*/
public function setInclude($include) {
$this->include = $include;
return $this;
}
/**
* Get include
*
* @return string
*/
public function getInclude() {
return $this->include;
}
/**
* Set gender
*
* @param string $gender
* @return member
*/
public function setGender($gender) {
$this->gender = $gender;
return $this;
}
/**
* Get gender
*
* @return string
*/
public function getGender() {
return $this->gender;
}
/**
* Set excludeDate
*
* @param \DateTime $excludeDate
* @return member
*/
public function setExcludeDate($excludeDate) {
$this->excludeDate = $excludeDate;
return $this;
}
/**
* Get excludeDate
*
* @return \DateTime
*/
public function getExcludeDate() {
return $this->excludeDate;
}
/**
* Set eid
*
* @param integer $eid
* @return member
*/
public function setEid($eid) {
$this->eid = $eid;
return $this;
}
/**
* Get eid
*
* @return integer
*/
public function getEid() {
return $this->eid;
}
种族实体:
class Ethnicity
{
public function __toString() {
return strval($this->id);
}
public function __construct()
{
$this->members = new ArrayCollection();
$this->clients = new ArrayCollection();
}
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $description
*
* @ORM\Column(name="description", type="string", length=45, nullable=false)
*/
private $description;
/**
* @var string $abbr
*
* @ORM\Column(name="abbr", type="string", length=5, nullable=true)
*/
private $abbr;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set description
*
* @param string $description
* @return Ethnicity
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set abbr
*
* @param string $abbr
* @return Ethnicity
*/
public function setAbbr($abbr)
{
$this->abbr = $abbr;
return $this;
}
/**
* Get abbr
*
* @return string
*/
public function getAbbr()
{
return $this->abbr;
}
}