0

I would like to know how to properly post data when Entity has another ManyToOne relation in FOSRestBundle.

User entity has locale (locale_id):

/**
 * @ORM\ManyToOne(targetEntity="Locale")
 * @ORM\JoinColumn(name="locale_id", referencedColumnName="id")
 */
private $locale;

I was hoping that passing something like:

{

    "user":{
        "firstName":"John",
        "emailAddress":"somewhere@somehow.com",
        "lastName":"Doe",
        "sex":"1",
        "locale":{
            "id":"1"
        }
    }

}

will work, but it does not pass the validation and Symfony throws:

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

As you can see, locale is still wrong.

Does anyone know how can I post it properly?

EDIT

Here is how the form looks like:

<?php

namespace Software\Bundle\Form\Type;

use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;

/**
 * Class UserType
 * @package Software\Bundle\Form\Type
 */
class UserType extends AbstractFormType
{
    public function buildForm(FormBuilderInterface $builder, array $option)
    {
        $builder
            ->add('firstName', 'text', [
                'label' => 'word.first_name',
                'required' => true
            ])
            ->add('lastName', 'text', [
                'label' => 'word.last_name',
                'required' => true
            ])
            ->add('emailAddress', 'email', [
                'label' => 'word.email_address',
                'required' => true
            ])
            ->add('sex', 'choice', [
                'label' => 'word.sex',
                'choices' => [
                    '0' => 'word.male',
                    '1' => 'word.female'
                ],
                'required'    => true,
                'empty_value' => 'word.select',
                'empty_data' => null
            ])
            ->add('locale', 'entity', [
                'label' => 'word.locale',
                'required'    => false,
                'property' => 'code',
                'class' => 'SoftwareBundle:Locale',
                'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('l')
                            ->orderBy('l.code', 'ASC');
                    },
                'placeholder' => 'word.select',
                'empty_data'  => null
            ])
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults([
            'translation_domain'    => 'general',
            'data_class'            => 'Software\Bundle\Entity\User',
            'attr'                  => ['novalidate' => 'novalidate'],
            'csrf_protection'       => false
        ]);
    }

    public function getName()
    {
        return 'user';
    }
}

EDIT 2

And the controller:

public function postAction(Request $request)
{
    $form = $this->createForm(new UserType(), new User());
    $form->handleRequest($request);

    if($form->isValid())
    {
        die('are you valid or not??');
    }

    return $this->view($form, 400);
}
undefinedman
  • 620
  • 1
  • 11
  • 25

1 Answers1

2

Try without the "1" and only with 1 , otherwise it can be interpreted as string.

Edit :

{

    "user":{
        "firstName":"John",
        "emailAddress":"somewhere@somehow.com",
        "lastName":"Doe",
        "sex":"1",
        "locale": 1
        }
    }

}
Community
  • 1
  • 1
Nawfal Serrar
  • 2,213
  • 1
  • 14
  • 22
  • Passing it as a integer also did not help – undefinedman Mar 17 '15 at 10:47
  • You may need to specify annotation if you didn't so that fos knows how to handle it , check this link http://www.tagwith.com/question_831804_fosrestbundle-how-to-create-a-new-entity-and-use-an-existing-one-in-manytoone/ – Nawfal Serrar Mar 17 '15 at 11:05
  • So I put this in my postAction @FosView(serializerEnableMaxDepthChecks=true, serializerGroups={"locale"}) but it did not resolve anything. Still validation complains. – undefinedman Mar 17 '15 at 11:43
  • can i have a look at your form? and have you tried creating a user without json? to make sure its a json problem not a mapping one – Nawfal Serrar Mar 17 '15 at 11:56
  • I will update my question and of course the form works properly if I just post it without json. – undefinedman Mar 17 '15 at 12:39
  • How do you handle the json in the action? – Nawfal Serrar Mar 18 '15 at 01:55
  • You tried giving the other properties of the locale entity? for exemple "user":{ "firstName":"John", "emailAddress":"somewhere@somehow.com", "lastName":"Doe", "sex":"1", "locale":{ "id":"1", "name":"en" } } } – Nawfal Serrar Mar 19 '15 at 02:19
  • 1
    you can also try "locale" : 1 without specifying id or name – Nawfal Serrar Mar 19 '15 at 02:50
  • It works!!! When I put locale: 1 without id or code, it passes the validation. But now I am wondering, what if I want to post user address, address is another table, I would like to have json like: "user":{"firstName":"John", "emailAddress":"somewhere@somehow.com", "lastName":"Doe", "sex":"1","locale":1, "address": { "street": "Some street", "city":"New York"}}}. I assume this way is not possible, do you know what would be the best way to accomplish that? Thanks man – undefinedman Mar 19 '15 at 12:07
  • You have a relationship with address and user right? i edit my answer so it would be correct with the question – Nawfal Serrar Mar 19 '15 at 12:10
  • Well I tried the way I thought it won't work but it actually works. So many thanks. I can continue with the API now. – undefinedman Mar 19 '15 at 13:59