symfony2.4 form 'このフォームに余分なフィールドのエラーを含めるべきではありません

StackOverflow https://stackoverflow.com//questions/23018907

質問

REST API 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(),
                )
        );
    }
}
.

マイタスクタイプフォーム:

 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))
        ;
    }
.

私のビューで私は1つのフォームだけをレンダリングしていますが、テスト段階にいる

{%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コントローラーがエラーをスローしています...

役に立ちましたか?

解決

Submitボタンを追加しているフォームを生成しているときは、それらを検証しているときはそうではありません。試してみてください:

public function cpostAction(Request $request)
{
    $entity = new Task();
    $form = $this->createForm(new TaskType(), $entity)->add('submit','submit');
    ...
.

SymfonyがデフォルトでEntityプロパティにマッピングされていても、[送信]ボタンは技術的にはフィールドです。したがって、送信ボタンでフォームを生成してから、検証コントローラのアクションで生成したフォームにフォームを送信する必要がある必要があります。

他のヒント

バリデータが追加のフィールドを無視する場合は、FormBuilderのオプションとして'allow_extra_fields' => trueを渡す必要があります。

フィールド検証を無効にしたい場合は、

を追加する必要があります。
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')
                .
                .
                .
    } 
.

詳細については: http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-dynamic-form-modification-suppressing-form-validation

単一のサブピットボタンを追加しているか類似している場合は、Solution 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,
            ))
.

複数のレベルがある場合は、すべてのレベルの埋め込みのためにそれを確実にすることを確認してください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top