將嵌套 Json 發(fā)送到 Symfony 表單
我有一個(gè)嵌套的 JSON 對(duì)象,我試圖將其發(fā)送到使用 FOSRestBundle 的 Symfony API。{ "firstName": "John", "lastName": "Doe", "email": "john.doe@gmail.com", "responses": [ {"1": "D"}, {"2": "B"}, {"3": "C"}, {"4": "F"} ]}但我收到以下錯(cuò)誤:{"code": 400,"message": "Validation Failed","errors": { "children": { "firstName": [], "lastName": [], "email": [], "responses": { "errors": [ "This value is not valid." ] } }}}這是我的表單類型:/** * @param FormBuilderInterface $builder * @param array $options */public function buildForm(FormBuilderInterface $builder, array $options){ $builder ->add('firstName', TextType::class, [ 'constraints' => [ new NotBlank(), new Length(['min' => 3]), ] ]) ->add('lastName', TextType::class, [ 'constraints' => [ new NotBlank(), new Length(['min' => 3]), ] ]) ->add('email', TextType::class, [ 'constraints' => [ new NotBlank(), new Length(['min' => 3]), ] ]) ->add('responses'); ;}這是我的控制器方法:/** * @Rest\Post( * path="/api/report" * ) * @param Request $request * @return Response */public function post(Request $request){ $form = $this->createForm(ReportType::class); $form->submit($request->request->all()); if (false === $form->isValid()) { return $this->handleView( $this->view($form) ); } return $this->handleView( $this->view( [ 'status' => 'ok', ], Response::HTTP_CREATED ) );}我很困惑,因?yàn)闆]有表單驗(yàn)證 $responses。我嘗試實(shí)現(xiàn)此鏈接上提供的解決方案: How to process Nested json with FOSRestBundle and symfony forms但我收到錯(cuò)誤“您無法將子項(xiàng)添加到簡單表單中”。也許您應(yīng)該將選項(xiàng)“compound”設(shè)置為 true?任何人都可以提供有關(guān)如何解決此問題的建議嗎?
查看完整描述