0

I am working on a Symfony 2.7 application that uses the Sonata admin bundle. I have a form defining a Video that allows users to select a Country of origin from a dropdown. Each user entity already has a Country of origin associated with it, so I merely want to give their country as a default value, with the option of selecting others. Here is the code as I have it now:

$formMapper
...
    ->add('countryOfOrigin',
          'country_of_origin',
          [ 'data' => $user->getOriginCountry() ])

The problem is that this code as it currently exists overwrites any previous selection -- making a user who is from Denmark always have 'Denmark' selected on form reload, even if they had previously told the application to associate the Video with 'Italy.'

How can I create a default dropdown value without overwriting what the user selects?

3
  • Do you want to change the origin country? Or is that an extra option you're saving somewhere in an entity? Commented Sep 4, 2018 at 14:09
  • do you try empty_data? Commented Sep 4, 2018 at 14:12
  • Thanks. After seeing your comment, I tried empty_data. It sort of seems to work, but I then get a different error when saving a new record: "Unable to reverse value for property path "countryOfOrigin": The choice "Denmark" does not exist or is not unique" Commented Sep 4, 2018 at 14:31

0

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.