我正在嘗試基於REST API Ang AngJJ構建應用程序。我一直在關注本教程http://npmasters.com/2012/11/25/Symfony2-Rest-FOSRestBundle.html,但必須更改一些細節(折舊方法),現在當我發佈創建新實體時,我得到'此表單不應包含額外字段'錯誤。Symfony2.4窗體'此表單不應該包含額外字段'的錯誤
class MainController extends Controller
{
public function indexAction(Request $request)
{
$form = $this->createForm(new TaskType(),null,array('action' => $this->generateUrl('post_tasks').'.json'))
->add('submit','submit');
$note_form = $this->createForm(new NoteType())
->add('submit','submit');
return $this->render('MyBundle:Main:index.html.twig',
array(
'form'=>$form->createView(),
'note_form'=>$note_form->createView(),
)
);
}
}
我的任務類型形式:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('timeStart','datetime',array(
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'date_format' => 'yyyy-MM-dd',
'data' => new \DateTime('now')
))
->add('timeStop','datetime',array(
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'date_format' => 'yyyy-MM-dd',
'data' => new \DateTime('now')
))
->add('project')
->add('descriptionTask')
->add('isCompleted',null,array('required' => false))
->add('isVisible',null,array('required' => false))
;
}
現在在我看來,我只渲染一個形式,但我在測試階段:
{%extends 'MyBundle::layout.html.twig' %}
{%block content %}
<div ng-view></div>
{{ form(form) }}
{% endblock %}
這是應該沖洗給定實體的REST控制器:
public function cpostAction(Request $request)
{
$entity = new Task();
$form = $this->createForm(new TaskType(), $entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirectView(
$this->generateUrl(
'get_organisation',
array('id' => $entity->getId())
),
Codes::HTTP_CREATED
);
}
return array(
'form' => $form,
);
}
奇怪的事情:當我把相同的代碼從REST控制器MainController,然後形式進行驗證和新實體被刷新,但不知何故,REST控制器拋出錯誤......
非常感謝你! – mmmm
非常感謝您的解決方案。你知道是否有解決方案不需要我在每個表單處理程序上執行此操作? – Jonathan
是的,而不是在事後添加提交添加到您的類型 – Chausser