2017-07-07 8 views
0

jsonリクエストからの値を持つ事前設定済みのエンティティを使用してフォームを作成すると、フォームが検証されない理由を理解しようとしています。ここでsymfony 2.7でREST json apiが埋め込まれていても検証されない

はFosRestBundleとsymfonyにおいて、コントローラがすでに設定されています。

public function createAction(Request $request) 
{ 
    $house = new House(); 
    $house->setTitle($request->get('title')); 
    $house->setDescription($request->get('description')); 
    $house->setPostcode($request->get('postCode')); 
    $house->setPhoneNumber((int) $request->get('phoneNumber')); 
    $availability = $request->get('available') ? true : false; 
    $house->setAvailability($availability); 

    $form = $this->createCreateForm($house); 

    $form->handleRequest($request); 

    $response = new JsonResponse(); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($house); 
     $em->flush(); 

     return $response->setData(array(
      'success' => true 
     )); 
    } 

    return $response->setData(array(
     'success' => false, 
     'errors' => $this->getFormErrors($form) 
    )); 
} 

private function createCreateForm(House $entity) 
{ 
    $form = $this->createForm(new HouseType(), $entity, array(
     'action' => $this->generateUrl('houses_create'), 
     'method' => 'POST', 
     'csrf_protection' => false 
    )); 

    return $form; 
} 

YAML設定ファイル:

# app/config/config.yml 
fos_rest: 
    param_fetcher_listener: true 
    body_listener: true 
    routing_loader: 
     default_format: json 
    exception: 
     enabled: true 
    # configure the view handler 
    view: 
     force_redirects: 
      html: true 
     formats: 
      json: true 
      xml: true 
     templating_formats: 
      html: true 
    # add a content negotiation rule, enabling support for json/xml for the entire website 
    format_listener: 
     enabled: true 
     rules: 
      - { path: ^/, priorities: [ json, xml, html ], fallback_format: html, prefer_extension: false } 

私は例えば$form->get('title')->getData()を実行した場合、私は、フォームが正しく満たされていることがわかりますしかし、まだ検証をパスしていないと私は$this->getFormErrors($form)を実行すると、私は空の配列を取得します。

どうすればこの問題をデバッグできますか?

答えて

1

jsonを受信するには、本体リスナー機能を有効にする必要があります。あなたのconfig.yml

fos_rest: 
    body_listener: true 

あなたがすべきもcheck the documentation for advanced usage

+0

答えていただきありがとうございます。その結果、yml設定ファイルが更新されましたが、引き続き同じ問題が発生しています。 – user2861867

+0

送信しているリクエストに "Content-Type:application/json"というヘッダーが含まれていますか? – magnetik

+0

sw0rdfishhhhhによると、私はちょうど最新のデバッグに応じて問題を編集しました。ありがとう – user2861867

0

$ form: "createCreateForm" - >これはちょうど "createForm"でなければなりません。

"if($ form-> isValid()){"の部分の後に$ form-> getData()の値をチェックしても問題が解決しない場合は、私は、フレームワークが要求を処理する方法を知らない(標準形式から来ていない)と考えています。その場合、エンティティの値を手動で設定して保存することができます。

$house->setStuff($request->request->get('stuff'); 
... 
$em->persist($house);`$em->flush();` 
+0

これを明確にするために欠けているメソッドを追加しました。 – user2861867

+0

$ form-> getData()の内容はどうですか?ちょうどvar_dump it.Ifが空であるか役立たない場合は、デバッガを使ってステップバイステップを試みてください(xdebugをセットアップしている場合)。ほとんどの場合、必要な値は$ request変数にあります。 – sw0rdfishhhhh

+0

今日はこのデバッグを行ったばかりです。値がフォームにあるようですが、 '$ form-> isValid()'はfalseを返し、 '$ this-> getFormErrors($ form)'を実行すると空の配列が返されます。 – user2861867

関連する問題