1

I have a nested JSON object that I am trying to send to a Symfony API which is using FOSRestBundle.

{
    "firstName": "John",
    "lastName": "Doe",
    "email": "[email protected]",
    "responses": [
        {"1": "D"},
        {"2": "B"},
        {"3": "C"},
        {"4": "F"}
    ]
}

But I get the following error:

{
"code": 400,
"message": "Validation Failed",
"errors": {
    "children": {
        "firstName": [],
        "lastName": [],
        "email": [],
        "responses": {
            "errors": [
                "This value is not valid."
            ]
        }
    }
}

}

This is my FormType:

/**
 * @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');
    ;
}

And this is my controller method:

/**
 * @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
        )
    );
}

I am confused as there is no form validation $responses.

I have tried to implement the solution offered on this link: How to process nested json with FOSRestBundle and symfony forms

But I get the error 'You cannot add children to a simple form. Maybe you should set the option "compound" to true?

Can anyone offer advice on how to resolve this?

1
  • the automatic "form validation" comes from the form type, which is dependent on how your form is defined. the Report entity's response property (getter/setter) might shed some light on it. If you don't set the form type, the form type will be guessed. The $request->request->all will probably deserialize the whole object, so you have an array of arrays for the responses field, which probably doesn't work for a automatically guessed form type...
    – Jakumi
    Aug 22, 2020 at 9:29

2 Answers 2

2

hello i think the issue is on responses. try using CollectionType. In this exemple using ChoiceType for each object in your collection. See here: 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',
     ],
 ],
]);
0

I know this wasn't really the question, but in case other people come here as I did after searching for how to put nested objects into FOSRestBundle annotations: I looked into my codebase and found the Symfony\Component\Validator\Constraints\Collection and Symfony\Component\Validator\Constraints\Composite constraints, which I think are going to serve me well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.