title: Symfony Security component register form ChoiceType field insert into bdd as an array(for the roles a user as)
body: <p>When i register with my form(1) the roles the user select isn't entered in the bdd instead it enter just [] i couldn't get it enter either ROLE_USER or ROLE_ADVERTISER or both in array in the bdd, roles is a longtext in,
  <a href="https://i.stack.imgur.com/KAtF1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KAtF1.png" alt="(1)" /></a></p>
  <p><a href="https://i.stack.imgur.com/uxIQf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uxIQf.png" alt="(2)" /></a></p>
  <p>Here is my Form builder:</p>
  <pre><code>class RegistrationFormType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('name')
              -&gt;add('agreeTerms', CheckboxType::class, [
                  'mapped' =&gt; false,
                  'constraints' =&gt; [
                      new IsTrue([
                          'message' =&gt; 'You should agree to our terms.',
                      ]),
                  ],
              ])
              -&gt;add('plainPassword', PasswordType::class, [
                  // instead of being set onto the object directly,
                  // this is read and encoded in the controller
                  'mapped' =&gt; false,
                  'constraints' =&gt; [
                      new NotBlank([
                          'message' =&gt; 'Please enter a password',
                      ]),
                      new Length([
                          'min' =&gt; 6,
                          'minMessage' =&gt; 'Your password should be at least {{ limit }} characters',
                          // max length allowed by Symfony for security reasons
                          'max' =&gt; 4096,
                      ]),
                  ],
              ])
              -&gt;add('roles', ChoiceType::class, [
                  'required' =&gt; true,
                  'multiple' =&gt; false,
                  'expanded' =&gt; false,
                  'choices' =&gt; [
                      'ROLE_USER' =&gt; 'ROLE_USER',
                      'ROLE_ADVERTISER' =&gt; 'ROLE_ADVERTISER',
                  ],
              ]);
          $builder-&gt;get('roles')
              -&gt;addModelTransformer(new CallbackTransformer(
                  function ($rolesArray) {
                      // transform the array to a string
                      return count($rolesArray)? $rolesArray[0]: null;
                  },
                  function ($rolesString) {
                      // transform the string back to an array
                      return [$rolesString];
                  }
          ));
      }
  
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults([
              'data_class' =&gt; User::class,
          ]);
      }
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/I7ply.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/I7ply.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/iN0DS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iN0DS.png" alt="enter image description here" /></a></p>