コレクション フィールドを持つフォームで、コレクションにエンティティ フィールド (メンバー タイプの 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;
}
}