我在页面上看到了一个实体的多个字段,我希望每个字段都可以通过 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->createForm(new $class, $entity);
使用这些代码行,我尝试创建生成表单字段的动态类,但由于它被转换为字符串并且我得到的错误是:
Fatal error: Class 'ragSocialType()' not found
但上课是!并且也被称为顶级文件..我希望我解释一下,我接受任何建议以遵循更好的方法!