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

select multiple setDefault

2 questions:

  1. How can I create select with multiple attribute in volt ?

{{ select_static('bon-cat[]', bon_cat, '', 'useEmpty': false, 'emptyText': '', 'class': 'form-control', "multiple":"multiple") }}

it creates select with multiple="multiple"... I want simple multiple.

2.How can I set default selected values (more than 1) for multi select?


$this->tag->setDefault("bon-cat", $bon_cat);

where $bon_cat is an array, gives me :

Fatal error: Uncaught exception 'Phalcon\Tag\Exception' with message 'Only scalar values can be assigned to UI components' in phalcon/dispatcher.zep:585 Stack trace: #0 /Users/IGonza/git//app/controllers/JobController.php(58): Phalcon\Tag::setDefault('bon-cat', Array) #1 [internal function]: JobController->addAction() #2 [internal function]: Phalcon\Dispatcher->dispatch() #3 /Users/IGonza/git/xxx/public/index.php(33): Phalcon\Mvc\Application->handle() #4 {main} thrown in phalcon/dispatcher.zep on line 585



51.2k

I don't use tags, since i create my forms extending \Phalcon\Forms\Form . I don't know if this will work for you, but here is what i have, and it's working for me:

        $signature = new Select('signatures[]', Signature::find(array(
                'is_active = 1',
                'order' => 'signature ASC'
            )), array(
            'using' => array(
                'id',
                'signature'
            ),
            'useEmpty' => false,
            'multiple' => true
        ));

        foreach ($entity->signatures->toArray() as $sig) {
            $sig_defaults[] = $sig['id'];
        }

        $signature->setDefault($sig_defaults);
        $this->add($signature);


10.7k

I didn't want to use php in volt file... but seems I have to :


<?php 
        echo $this->tag->select(array(
            "bon-cat",
            'name' => "bon-cat[]",
            "class" => "form-control",
            'tabindex' => '',
            "multiple" => "multiple",
            $bon_cat,
            'value' => !empty($job->bon_cat) ? $job->bon_cat : '',
            'useEmpty' => false,
            'emptyText' => '&nbsp;',
            'emptyValue' => '0'));   ?>

and in controller I can use :


$job->bon_cat = $this->request->getPost('bon-cat');

That works fine.