genemu / GenemuFormBundle

Extra Form : Captcha GD, Tinymce, Recaptcha, JQueryDate, JQueryAutocomplete, JQuerySlider, JQueryFile, JQueryImage
587 stars 265 forks source link

illegal Offset Type #411

Open Sashkan opened 8 years ago

Sashkan commented 8 years ago

I'm struggling with a genemu_jqueryselect2_choice

->add('departure', 'genemu_jqueryselect2_choice', array(
                'choices' => $airports,
                'label' => 'Departure',
            ))

in this case, $airports is an array of stdClass objects, which look like this :

array:1711 [▼
  0 => {#561 ▼
    +"lfi": "XXXXXXXX1"
    +"icao": "ENEG"
    +"iata": null
    +"faaCode": null
    +"name": " Eggemoen"
    +"city": " Eggemoen"
    +"countryCode": "FR"
    +"latitudeDecimal": "-60.2272220"
    +"longitudeDecimal": "10.3058330"
    +"utcStdConversion": null
  }
  1 => {#562 ▼
    +"lfi": "SP80307"
    +"icao": "LECO"
    +"iata": "LCG"
    +"faaCode": null
    +"name": "A CORUNA"
    +"city": "LA CORUNA"
    +"countryCode": "ES"
    +"latitudeDecimal": "43.3020611"
    +"longitudeDecimal": "-8.3772556"
    +"utcStdConversion": "+1"
  }
... 

This causes an error : Warning: Illegal offset type

How can I use this array as a select2 choice input ?

WedgeSama commented 8 years ago

This is not a GenemuFormBundle specific case. If you try your code with the basic choice type, it normal throw the same warning.

In your case, I asume you do not want to use optgroup. So you have to give in place of the choices option, an associative array of key => string. And stdClass is not a string. If you want to give objects, define the method public function __toString() in your class (so create one for this and do not use stdClass). In this method, return the string you want to be display in the select input to represente your object.

At this time (maybe in a next release of Symfony), if you want to pass more then just a string, you cannot do that with default choice form type (same for genemu_jqueryselect2_choice).