我正在尝试基于REST api ang AngularJS构建应用程序。我一直在关注这个教程http://npmasters.com/2012/11/25/Symfony2-Rest-FOSRestBundle.html但是必须更改一些细节(折旧方法),现在当我发布创建新实体时,我得到'此表单不应包含额外字段'错误。
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(),
)
);
}
}
我的TaskType表单:
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,
);
}
WEIRD THING:当我将相同的代码从REST控制器放到MainController,然后表单被验证并且正在刷新新实体,但不知何故REST控制器抛出错误...
答案 0 :(得分:25)
如果您希望验证器忽略其他字段,您应该尝试将'allow_extra_fields' => true
作为选项传递给FormBuilder。
答案 1 :(得分:16)
因为当您生成表单时,您正在添加提交按钮,但是当您验证它们时,您不是。尝试:
public function cpostAction(Request $request)
{
$entity = new Task();
$form = $this->createForm(new TaskType(), $entity)->add('submit','submit');
...
提交按钮在技术上是一个字段,即使symfony默认情况下不会将其映射到实体属性。因此,当您使用提交按钮生成表单然后提交该表单时,您在验证控制器中生成的表单操作还需要一个提交按钮。
答案 2 :(得分:3)
如果要禁用字段验证,则必须添加
public function setDefaultOptions(\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'csrf_protection' => false,
'validation_groups' => false,
));
}
在buildForm方法中:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->addEventListener(FormEvents::POST_SUBMIT, function ($event) {
$event->stopPropagation();
}, 900);
$builder->add('field1','text')
->add('field2','text')
.
.
.
}
答案 3 :(得分:1)
如果要添加单个SubmitType按钮或类似按钮,请使用Chausser指示的解决方案
$entity = new Task();
$form = $this->createForm(new TaskType(), $entity)->add('submit','SubmitType::class');
但是,如果您使用CollectionType并嵌入一组子表单,则需要在该类型的参数中包含'allow_add'=> true。例如,在您的EntityType表单构建器中:
$builder->add('subEntities', CollectionType::class, array(
'entry_type' => SubEntityType::class,
'entry_options' => array('label' => false),
'allow_add' => true,
))
如果您有多个级别,请确保对所有级别的嵌入都执行此操作。
答案 4 :(得分:1)
在CustomFormType中使用Symfony 4/5
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => CustomFormType::class,
'allow_extra_fields' => true // <<<<<<<<<<<<<<<<<<<<<
]);
}