We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon\Tag, select multiple - assign defaults from controller

I need to assign several default values to select multiple tag. At the moment, I managed to assign only one value with

$tag->setDefault('element[]', 'value');

If I try to pass array as value, it tells me that only scalar values are allowed. Any way to achieve that?



51.2k
edited Jun '14
        $tag = new Select('element[]', array(1,2,3,4,5), array(
            'useEmpty' => false,
            'multiple' => true
        ));

        $tag->setDefault(array(1,4));


19.2k

Nope, that doesn't works... Here's the code from view:

{{ select("estate_types[]", estate_type, "using": ["id","name"], "useEmpty":false, "multiple":true) }}

And from controller:

$this->tag->setDefault('estate_types[]', array(1,2));

This gives me that:

Phalcon\Tag\Exception: Only scalar values can be assigned to UI components


19.2k
Accepted
answer
edited Jul '14

Solved it via Phalcon\Forms\Element\Select()->setDefault(array())

How to apply it?

For others returning to this question, the only setDefault in the system which complains about arrays is the setDefault associated with tag: https://github.com/phalcon/cphalcon/blob/107b6f0197b0464d9f6b11ff390a560c000bf448/phalcon/tag.zep#L252 You can work around this and use chebureque's hacky solution, or you can use tag's setDefaults (plural) which is designed for working with arrays. It also accepts a second parameter in case you want to array_merge the array previously set