2017-11-07 12 views
2

ajaxを使用してSymfony3エンティティを削除しようとしています。Symfony 3:Ajaxを使用してコントローラを呼び出すdeleteAction

問題は$form->isValid()はfalseを返しますが、フォーム(または子要素)にエラーはありません。私はここで何が欠けていますか?

コントローラ

/** 
* @Route("/{profileID}/delete", name="profile_delete") 
* @ParamConverter("Profile", options={"mapping": {"profileID": "id"}}) 
* @Method("DELETE") 
*/ 
public function deleteAction(Request $request, Profile $Profile) 
{ 
    $form = $this->createDeleteForm($Profile); 
    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->remove($Profile); 
     $em->flush(); 

     return new JsonResponse(array('message' => 'profile removed')); 
    } else { 
     return new JsonResponse(array('message' => 'error')); 
    } 
} 

private function createDeleteForm(Profile $Profile) 
{ 
    return $this->createFormBuilder() 
     ->setAction($this->generateUrl('profile_delete', array('profileID' => $Profile->getId()))) 
     ->setMethod('DELETE') 
     ->getForm() 
    ; 
} 

小枝

$.ajax({ 
    url: "{{ path('profile_delete', {'profileID': Profile.id}) }}", 
    type: 'delete', 
    success:function(data){ 
    console.log(data); 
    }, 
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
    console.log(textStatus); 
    console.log(errorThrown); 
    } 
}); 
+0

あなたはプロファイラでno形式/ログの問題を抱えていますか? – goto

+0

私の推測では、csrfトークンが不足していますが、必須です。 – Yoshi

+0

@goto no、プロファイラは問題を報告しません。 @ Yoshiどのようにトークンを提出する必要がありますか?私はフォームデータ全体をシリアル化して送信しようとしましたが、 'data:{_token:value} 'と同じ結果を返しました。 –

答えて

1

あなたはCSRFトークンせずにフォームを送信しています。クイックフィックスは、データとしてトークンを追加することです:ここ

$.ajax({ 
    url: "{{ path('profile_delete', {'profileID': Profile.id}) }}", 
    type: 'delete', 
    data: { 
     form: { 
      _token: "{{ csrf_token('form') }}" 
     } 
    }, 
    success:function(data){ 
     console.log(data); 
    }, 
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
     console.log(textStatus); 
     console.log(errorThrown); 
    } 
}); 

formフォーム名であり、また、手動で設定することができます。例えば。 delete-form

private function createDeleteForm(Profile $Profile) 
{ 
    return $this 
     ->get('form.factory') 
     ->createNamedBuilder('delete-form', FormType::class, null, [ 
      'action' => $this->generateUrl('profile_delete', array('profileID' => $Profile->getId())), 
      'method' => 'DELETE', 
     ]) 
     ->getForm() 
    ; 
} 

と:

$.ajax({ 
    url: "{{ path('profile_delete', {'profileID': Profile.id}) }}", 
    type: 'delete', 
    data: { 
     'delete-form': { 
      _token: "{{ csrf_token('delete-form') }}" 
     } 
    }, 
    success:function(data){ 
     console.log(data); 
    }, 
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
     console.log(textStatus); 
     console.log(errorThrown); 
    } 
}); 
+0

ああ、ありがとう!実際にフォーム名はトークンと共にリクエストに含まれている必要があります –

+0

フォームエラーがないのは奇妙なことではありませんか? _tokenはそのエラーをフォームの親にバインドすることを覚えています – goto

+1

@goto投稿データに*フォーム名*がなければ、フォームは正式に提出されませんでした。このようにエラーチェックはまだ行われていませんでした。私たちが 'data:{form:{}}'を提出すると、トークンのエラーが表示されるはずです。 – Yoshi

関連する問題