Symfony2:允许FormType类中的额外字段

时间:2014-03-18 01:39:15

标签: php symfony symfony-forms

因此,在阅读docs并在我的代码中应用相同的概念后,我仍然会收到错误:

  

此表单不应包含额外字段

以下是生成错误的表单的代码:

namespace Common\CommonBundle\Form;

use Symfony\Component\Form\AbstractType,
    Symfony\Component\Form\FormBuilderInterface,
    Symfony\Component\OptionsResolver\OptionsResolverInterface,
    Common\CommonBundle\Form\AddressExtraInfoType;

class StandardAddressType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('extra_info', new AddressExtraInfoType());
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Common\CommonBundle\Entity\StandardAddress',
            'cascade_validation' => true
        ));
    }

    public function getName() {
        return 'standard_address';
    }

}

我的代码中的错误在哪里?

添加一些遗漏信息

为了回答为什么我说错误发生在那个文件中(对我来说),这是我在$form->isValid()传递时返回的JSON的结果:

{
  "success": false,
  "errors": {
    "formCompany": [
      "This form should not contain extra fields."
    ],
    "formStandardAddress": [
      "This form should not contain extra fields."
    ],
    "formPhone": [],
    "formCourrierAddress": []
  }
}

错误发生在formCompanyformStandardAddress中。下面是StandardAddress.php的实体(我删除了非必需的方法,只留下相关部分):

<?php

namespace Common\CommonBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Fresh\Bundle\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Common\CommonBundle\Entity\Country;
use Common\CommonBundle\Entity\State;
use Common\CommonBundle\Entity\City;
use Common\CommonBundle\Entity\AddressExtraInfo;

/**
 * @ORM\Entity
 * @ORM\Table(name="standard_address")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 */
class StandardAddress {

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="City")
     * @ORM\JoinColumn(name="city", referencedColumnName="iso", nullable=false)
     */
    protected $city;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Country")
     * @ORM\JoinColumn(name="country", referencedColumnName="iso", nullable=false)
     */
    protected $country;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="State")
     * @ORM\JoinColumn(name="state", referencedColumnName="iso", nullable=false)
     */
    protected $state;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="AddressExtraInfo")
     * @ORM\JoinColumn(name="address_extra_info", referencedColumnName="id", nullable=false)
     */
    protected $extra_info;

    /**
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="created", type="datetime")
     */
    protected $created;

    /**
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="modified", type="datetime")
     */
    protected $modified;

    /**
     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
     */
    protected $deletedAt;

    public function setExtraInfo(AddressExtraInfo $extra_info) {
        $this->extra_info = $extra_info;
    }

    public function getExtraInfo() {
        return $this->extra_info;
    }

}

请注意,里面有extra_info

1 个答案:

答案 0 :(得分:1)

字段extra_info应在'Common\CommonBundle\Entity\StandardAddress'类中引用;和相应的geter和setter为该领域。在这些实体之间建立任何关系都会更好。

您的StandAddress实体拥有像

这样的方法
    /**
     * @Assert\Type(type="Common\CommonBundle\Entity\extra_infoEntity")
     * the entity referring extra_info
     */
    protected $extraInfo;

    // ...

    public function getExtraInfo()
    {
        return $this->extraInfo;
    }

    public function setExtraInfo(extra_infoEntity $extraInfo = null)
    {
        $this->extraInfo = $extraInfo;
    }

然后,只有您可以在当前FormType

中导入另一个FormType

您可以尝试在实体注释中使用这些更改

       /**
         * @ORM\Id
         * @ORM\ManyToOne(targetEntity="AddressExtraInfo")
         * @ORM\JoinColumns({
         *    @ORM\JoinColumn(name="address_extra_info", referencedColumnName="id",nullable=false)
         * })
         */
        protected $extra_info;

@ORM\JoinColumn

中注释@ORM\JoinColumns

这种类型的实施对我有用。

有关完整参考,请参阅DOCUMENTATION