您好我试图制作动态表单并且我遇到了一些问题。我一直在尝试关注http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html和一些相关的教程,但没有结果。
目前我收到以下错误:
可捕获的致命错误:参数2传递给 UserBundle \表格\型号\ CompanyRegistrationFormType :: UserBundle \表格\ {型封}() 必须是UserBundle \ Entity \ Sector的实例,实例 给出UserBundle \ Entity \ Company,调用 UserBundle / Form / Type / CompanyRegistrationFormType.php在第82行并定义了
我有3个实体表:
Company.php
/**
* @ORM\OneToMany(targetEntity="Sector", mappedBy="company")
*/
protected $sector;
Sector.php
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="name", type="string")
*/
protected $name;
/**
* @ORM\ManyToOne(targetEntity="Company", inversedBy="sector")
*/
protected $company;
/**
* @ORM\OneToMany(targetEntity="MainCategory", mappedBy="sector")
*/
protected $mainCategory;
MainCategory.php
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $name;
/**
* @ORM\ManyToOne(targetEntity="Sector", inversedBy="mainCategory")
*/
protected $sector;
FormType.php
$builder->add('sector', 'entity', array(
'class' => 'UserBundle:Company'));
$formModifier = function (FormInterface $form, Sector $sector = null) {
$mainCategories = null === $sector ? array() : $sector->getAvailableMainCategories();
$form->add('mainCategory', 'entity', array(
'class' => 'UserBundle:Sector',
'choices' => $mainCategories));
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
$data = $event->getData();
$formModifier($event->getForm(), $data);
});
$builder->get('sector')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
$sector = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $sector);
});
和树枝:
{{ form_row(form.sector) }} {# <select id="company_sector" #}
{{ form_row(form.mainCategory) }} {# select id="company_mainCategory" #}
<script>
var $sector = $('#company_sector');
$sport.change(function(){
var $form = $(this).closest('form');
var data = {};
data[$sector.attr('name')] = $sector.val();
$.ajax({
url : $form.attr('action'),
type : $form.attr('method'),
data : data,
success: function(html) {
$('#company_mainCategory').replaceWith(
$(html).find('#company_mainCategory'));
}
});
});
</script>
第82行
$formModifier($event->getForm(), $data);
如果我将其更改为
$formModifier($event->getForm(), $data->getSector());
我收到以下错误:
有人可以帮帮我吗?谢谢你的时间!注意:未定义的属性: UserBundle \实体\公司:: $ mainCategory
答案 0 :(得分:1)
$builder->add('sector', 'entity', array(
'class' => 'UserBundle:Company'));
你必须传入与实体Sector相关的类参数classname(我想UserBundle:Sector
)