1

I'm testing some "API developing" methods in Symfony 3.3

I just run through a tutorial, which told me that I could use Symfony Forms to:

  1. Automatically validate JSON;

  2. Automatically fill the new entity with the JSON data.

So I created a method "POST", which I use to create a resource, say USER.

  1. I POST to that method with the following rawbody: {"name": "foo", "surname": "bar", "userType": 1}

  2. The tutorial said I could do those two "automatic things" with form, by writing the following lines of code:

    $data = json_decode($request->getContent(), true);
    
    $user = new User();
    
    $form = $this->createForm(UserType::class, $user );
    
    $form->submit($data);
    
    $em = $this->getDoctrine()->getManager();
    
    $em->persist($user);
    
    $em->flush();
    

The form class "UserType" has the following lines of code:

namespace FooBundle\Form;

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class)
            ->add('surname', TextType::class)
            ->add('userType', ChoiceType::class, array(
                    'choices' => array(
                        1 => 'one',
                        2 => 'two',
                        3 => 'three',
                        4 => 'four',
                        5 => 'five',
                        6 => 'six',
                    )
                )
            )
        ;
    }


    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            array(
                "data_class" => "FooBundle\Entity\User"
            )
        );

    }

}

When I post to that endpoint, however, I have the following error:

An exception occurred while executing 'INSERT INTO user(name, surname, user_type) VALUES (?, ?, ?)' with params ["foo", "bar", null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_type' cannot be null

Why does the form does not automatically fill in the "user type" field?

Did I do something wrong in the "ChoiceType" field of the form?

Thanks :)

1 Answer 1

1

The options of your ChoiceType field are inverted. Your choices option array should be the over way around as explained in the documentation:

The choices option is an array, where the array key is the item's label and the array value is the item's value

As it is, when your form handles the request data, it stumbles upon an integer which does not belong to the expected values for the userType. This case is handled by simply ignoring the given value which is why you end up with a null value.

Long story short, you have to write your choices array like this in your case:

'choices' => ['one' => 1, 'two' => 2, ...]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.