2

Trying to validate a choice field (multiple checkboxes) Im having this problem:

"Notice: Array to string conversion "

My validation file looks like this one:

Cgboard\AppBundle\Forms\UploadImageEntity:
  properties:
    image:
          ... 
    cgnetworks:
      - Choice:
          choices: [flickr, tumblr]  //<--- this is giving me problems!!!

My form entity class (Im not going to save this to db for now):

class UploadImageEntity {
    public $image;
    public $cgnetworks;
} 

And my form class:

class UploadImageForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('image', 'file')
            ->add('cgnetworks', 'choice', [
                    'choices'  => $this->getCgNetworks(),
                    'multiple' => TRUE,
                    'expanded' => TRUE
                ]
            );
    }

    public function getCgNetworks()
    {
        return [
        'tumblr' => 'Tumblr',
        'flickr' => 'Flickr'
        ];
    } 

} 

Any idea?

Francisco Albert
  • 1,577
  • 2
  • 17
  • 34
  • It looks like you have a two dimensional array there, with 'Choice' being the index for the first key, and 'choices' being the second. – Lighthart May 28 '14 at 00:06
  • yes, but in the doc seems to be fine... http://symfony.com/doc/current/reference/forms/types/choice.html – Francisco Albert May 28 '14 at 00:10
  • 1
    Best guess: https://github.com/symfony/symfony/issues/7759. This might also be useful: http://stackoverflow.com/questions/17314996/symfony2-array-to-string-conversion-error – Lighthart May 28 '14 at 00:17
  • I already read that, thanks anyway, but they use the entity trying to save in the db, I don't want to save anything for now, just validate it – Francisco Albert May 28 '14 at 00:20

2 Answers2

3

perhaps you need to specify multiple in your validation

cgnetworks:
  - Choice:
      choices: [flickr, tumblr]  //<--- this is giving me problems!!!
      multiple: true
Derick F
  • 2,749
  • 3
  • 20
  • 30
0

Check your Entity field getter. If you have something else instead of

public function getValue(){
      return $this->value;
}

You can reach this error.

Form builder uses get and set entity methods, that's why you need to return an allowable value.

Dapter20
  • 353
  • 4
  • 5