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)
を実行すると、私は空の配列を取得します。
どうすればこの問題をデバッグできますか?
答えていただきありがとうございます。その結果、yml設定ファイルが更新されましたが、引き続き同じ問題が発生しています。 – user2861867
送信しているリクエストに "Content-Type:application/json"というヘッダーが含まれていますか? – magnetik
sw0rdfishhhhhによると、私はちょうど最新のデバッグに応じて問題を編集しました。ありがとう – user2861867