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

Populating a multiple select value with previously selected values

Hello,

I currently have a select dropdown (multiple)

Here is how i display the select in the volt view file

 {{ select('inbox-user[]', getUsers, 'using': ['id_admin', 'name_admin'], 'multiple' : 'multiple', 'class' : 'form-control select2') }}

Then in the inboxController i pull the currently selected values from the database

$this->view->getUsers = AdminUser::find();

//get a list of all the users currently attached to this inbox
$users = AdminInboxes::find('id_inboxes = '.$row->getIdInbox());

foreach ($users as $user){
    $user_ids[] = $user->getIdAdmin();
}

So i end up with an array for $user_ids like this:

array(3) {
    [0]=>
    string(2) "26"
    [1]=>
    string(2) "28"
    [2]=>
    string(2) "29"
}

But when i run the query i get this error

phalcon Only scalar values can be assigned to UI components

Now if found a solution here:

How ever i dont understand how to apply that to my exampleas it doesnt reference the id of the selectbox in that example.Should it be something like this:

Phalcon\Forms\Element\Select()->setDefault(array([$user_ids]))

Or do i put the id of the select inside Select([$users_ids])

Any help very much appreciated, i am also open to doing it a different way as long as it works.

One other thing, i notice in the volt file i am using ['id_admin', 'name_admin'] but in the setDefault example i am just passing the array of Ids, do i also need to pass the name too?



33.8k

If you create an element in the view, the action doesn't know of its existence. You'll need to pass the default values from the action to the view and assign then to the SELECT.