第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

將嵌套 Json 發(fā)送到 Symfony 表單

將嵌套 Json 發(fā)送到 Symfony 表單

PHP
紅糖糍粑 2023-10-22 21:34:05
我有一個(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)如何解決此問題的建議嗎?
查看完整描述

2 回答

?
江戶川亂折騰

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊

你好,我認(rèn)為問題出在回應(yīng)上。嘗試使用 CollectionType。在此示例中,對(duì)集合中的每個(gè)對(duì)象使用 ChoiceType。請(qǐng)參閱此處:https ://symfony.com/doc/current/reference/forms/types/collection.html#entry-options

->add('responses', CollectionType::class, [

 'entry_type'   => ChoiceType::class,

 'entry_options'  => [

     'choices'  => [

         '1' => 'D',

         '2' => 'A',

     ],

 ],

]);


查看完整回答
反對(duì) 回復(fù) 2023-10-22
?
慕桂英546537

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊

我知道這不是真正的問題,但萬一其他人像我一樣在搜索如何將嵌套對(duì)象放入 FOSRestBundle 注釋后來到這里:我查看了我的代碼庫并找到了 和 約束,我認(rèn)為它們將Symfony\Component\Validator\Constraints\Collection提供Symfony\Component\Validator\Constraints\Composite服務(wù)我很好。



查看完整回答
反對(duì) 回復(fù) 2023-10-22
  • 2 回答
  • 0 關(guān)注
  • 140 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)