[英]Symfony2: allow extra fields in FormType class
So after read the docs and apply just the same concepts in my code I'm still getting the error: 因此,在阅读了文档并在代码中应用了相同的概念之后,我仍然遇到错误:
This form should not contain extra fields
此表单不应包含其他字段
Below is the code for the form that generates the error: 下面是生成错误的表单的代码:
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';
}
}
Where is the error in my code? 我的代码中的错误在哪里?
Add some missed info 添加一些错过的信息
In answer to why I said error happen in that file (for me) here is the result of a JSON I return if $form->isValid()
pass: 为了回答为什么我说该文件中发生错误(对我来说),这是如果
$form->isValid()
通过的话,我返回的JSON结果:
{
"success": false,
"errors": {
"formCompany": [
"This form should not contain extra fields."
],
"formStandardAddress": [
"This form should not contain extra fields."
],
"formPhone": [],
"formCourrierAddress": []
}
}
Errors happen in formCompany
and formStandardAddress
. 错误发生在
formCompany
和formStandardAddress
。 Below is the entity for StandardAddress.php
(I removed non required methods, just leave the relevant part): 以下是
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;
}
}
Notice there is a extra_info
inside it. 注意里面有一个
extra_info
。
The field extra_info
should be referred in 'Common\\CommonBundle\\Entity\\StandardAddress'
class; 字段
extra_info
应该在'Common\\CommonBundle\\Entity\\StandardAddress'
类中'Common\\CommonBundle\\Entity\\StandardAddress'
; and corresponding geter and setter for the field. 以及该领域相应的获取器和设置器。 It's better to have any relation between these entities.
这些实体之间最好有任何关系。
Your StandAddress
entity mus have methods like, 您的
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;
}
Then only you can import another FormType
within your current FormType
那么只有你可以导入另一个
FormType
当前的内FormType
You may Try with these changes in your Entity annotation 您可以尝试在Entity批注中进行这些更改
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="AddressExtraInfo")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="address_extra_info", referencedColumnName="id",nullable=false)
* })
*/
protected $extra_info;
Annotate @ORM\\JoinColumn
inside @ORM\\JoinColumns
注释
@ORM\\JoinColumn
内@ORM\\JoinColumns
This type of implementation worked for me. 这种实现方式对我有用。
For complete reference see the DOCUMENTATION 有关完整参考,请参见文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.