Symfony2:集合验证导致实体字段错误

时间:2012-12-21 23:31:54

标签: validation symfony

在具有集合字段的表单中,集合中包含实体字段(成员类型中的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;
    }
}

2 个答案:

答案 0 :(得分:0)

您的会员实体是否与您的种族实体有关系?从名称“eid”来看,您似乎试图将种族类强制为整数字段(“eid”)。

如果没有看到这两个实体,很难找到任何东西。

答案 1 :(得分:0)

原来我的错误是假设集合字段需要集合验证器。相反,验证器只需要在构成集合的实体上。在这种情况下,我真正需要做的就是在Member实体中指定验证(并删除客户端实体中的注释断言。)当我简化了种族实体字段时,处理得当。所以我的错误还包括Symfony更复杂的概念 - 并非总是如此,有时候也是如此!