我在页面上看到了一个实体的许多字段,我希望每个字段都可以通过ajax编辑它们,一次一个。 为此,我提出了为所有领域构建一个独特的控制器的想法,但我不能使它工作,我不知道它是否是我尝试做的正确的解决方案。 我的页面显示字段:
<div>
<form class="ajax" action="{{ path('ajax_setSocial', { 'id': entity.id }) }}" method="post" {{ form_enctype(form) }}>
<div class="editor">
{{ form_errors(form) }}
<div class="editLabel pls lti">{{ form_label(form.ragSocial) }}</div>
<div class="editField">
<div class="ptm">
{{ form_widget(form.ragSocial) }} {{ form_errors(form.ragSocial) }}
</div>
{{ form_widget(form._token) }}
<div class="mtm">
<button class="btn btn-primary disabled save" type="submit" disabled>Save</button>
<button class="btn ann">Cancel</button>
</div>
</div>
</div>
</form>
</div>
<div>
<form class="ajax" action="{{ path('ajax_setSocial', { 'id': entity.id }) }}" method="post" {{ form_enctype(form) }}>
<div class="editor">
{{ form_errors(form) }}
<div class="editLabel pls lti">{{ form_label(form.pIva) }}</div>
<div class="editField">
<div class="ptm">
{{ form_widget(form.pIva) }} {{ form_errors(form.pIva) }}
</div>
{{ form_widget(form._token) }}
<div class="mtm">
<button class="btn btn-primary disabled save" type="submit" disabled>Save</button>
<button class="btn ann">Cancel</button>
</div>
</div>
</div>
</form>
</div>
在我的控制器中:
public function setSocialAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MyBusinessBundle:Anagrafica')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Anagrafic entity.');
}
$field = $request->get('field');
$class = $field.'Type()';
$form = $this->createForm(new $class, $entity);
$form->bind($request);
if ($form->isValid()) {
$em->persist($entity);
$em->flush();
$response = new Response();
$output = array('success' => true);
$response->headers->set('Content-Type', 'application/json');
$response->setContent(json_encode($output));
return $response;
}
$ class = $ field.'Type()';
$ form = $ this-&gt; createForm(new $ class,$ entity);
使用这些代码行我尝试创建生成表单字段的动态类,但不起作用,因为它被转换为字符串,我得到的错误是:
Fatal error: Class 'ragSocialType()' not found
但课程是!并且也被调用到顶级文件.. 我希望我解释,我接受任何建议,以更好的方式!
答案 0 :(得分:1)
$class = $field.'Type'; //remove the ()
$form = this->createForm(new $class, $entity);