Using multiple value fields (checkbox), selection not stored

Hi all ,
I have first time created a form with allowing multiple selections on a field.
The html output looks like it should with a hidden field and several checkboxes.
The controller function is baked standard for add an edit. But I receive no data in the datebase.
Hier are the functions:

add.ctp

<?php $array = $this->ViewTools->getallparameter('P', $product); ?> <?= $this->Element('cssTop', array('large' => 12, 'medium' => 9, 'product' => $product, 'name' => $array)); ?>
<?= $this->Form->create($user) ?>
<fieldset>
    <legend><?= __('Erstellen') ?></legend>
    <ul class="tab">
        <li><a href="javascript:void(0)" class="tablinks" onclick="openTab(event, 'London')" id="defaultOpen">Name</a></li>
        <li><a href="javascript:void(0)" class="tablinks" onclick="openTab(event, 'Paris')">Adresse</a></li>
        <li><a href="javascript:void(0)" class="tablinks" onclick="openTab(event, 'Tokyo')">Wohnung</a></li>
    </ul>
    <div id="London" class="tabcontent">
    <?php
        echo $this->Form->input('last_name',['id' => 'last']);
        echo $this->Form->input('first_name',['id' => 'first']);            
        echo $this->Form->input('username',['id' => 'total']);
        echo $this->Form->input('birthday', array(
        'label' => 'Geburtstag', 
        'dateFormat' => 'DMY',
        'minYear' => date('Y') - 80,
        'maxYear' => date('Y') - 2 ));
       
        $options = [
        '1' => 'ledig',
        '2' => 'verheiratet',
            '3' => 'verwitwet',
        ];
        echo $this->Form->select('civil_status', $options);            
        echo $this->Form->input('password');
        echo $this->Form->input('userfunction_id', ['options' => $userfunctions]); ?>
    </div>
    <div id="Paris" class="tabcontent">
    <?php
        echo $this->Form->input('company');
        echo $this->Form->input('street', ['default' => 'Schillerstraße']);
        echo $this->Form->input('no');
        echo $this->Form->input('city', ['default' => 'Ginsheim-Gustavsburg']);
        echo $this->Form->input('zip', ['default' => '65462']);
        echo $this->Form->input('phone');
        echo $this->Form->input('mobile');
        echo $this->Form->input('fax');
        echo $this->Form->input('email');
        echo $this->Form->input('iban');
        echo $this->Form->input('bic'); ?>
       </div>
    <div id="Tokyo" class="tabcontent">
     <?php
        $options = [
            '0' => 'kein Status',
            '1' => 'aktiv',
            '2' => 'passiv',
            '3' => 'ausgetreten',                
        ];
        echo $this->Form->radio('status', $options);
        echo $this->Form->label('User.member', 'Mitglied'); 
        $options = [
            '1' => 'Mitglied',
            '2' => 'Interessent',
            '3' => 'Ehemaliger',
            '4' => 'Bewohner',
            '5' => 'Partner',
            '6' => 'Warteliste',
            '7' => 'Gärtner',
        ];
        //echo $this->Form->select('member', ['options' => $mamberList , 'multiple' => true] );
        echo $this->Form->select('member', $options, ['multiple' => 'checkbox']);

        echo $this->Form->input('userappartment_id', ['options' => $userappartments]);
        echo $this->Form->input('userparking_id', ['options' => $userparkings]);
        echo $this->Form->input('contact');
        
     ?>
    </div>
</fieldset>
<?= $this->Form->button(__('Speichern')) ?>
<?= $this->Form->end() ?>
<?= $this->Element('jsUsers'); ?>

UsersController:

public function add() {
    $this->set('userfunc', $this->Auth->user('userfunction_id'));
    $user = $this->Users->newEntity();
    if ($this->request->is('post')) {
        
        $user = $this->Users->patchEntity($user, $this->request->data);

        if ($this->Users->save($user)) {
            $this->Flash->success(__('The user has been saved.'));

            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
        }
    }
    $userfunctions = $this->Users->Userfunctions->find('list', ['limit' => 200]);
    $userappartments = $this->Users->Userappartments->find('list', ['limit' => 200]);
    $userparkings = $this->Users->Userparkings->find('list', ['limit' => 200]);
    $this->set(compact('user', 'userfunctions', 'userappartments', 'userparkings'));
    $this->set('_serialize', ['user']);
}

I am wondering, if I need to join the values with a seperator before storing in the database.
Has anyone a tip or suggestion please??

Regards Klaus

Yes, if I remember correctly, multi-select boxes will be available as an array in $this->request->data and thus won’t be saved in a single field. You can either go with the HABTM approach or in this case perhaps it would be simpler to just implode the array or save it as json to avoid having to deal with other db tables.

Hello, thanks I have included an ‘beforeMarshal’ and it looks fine.
It is easier as assumed nad it is fun to work with cake :heart_eyes:

Please tell me, where is my fault. I can’t see any difference to the solution I found.

Since I am now able to use checkbox data with multiple values, I am wondering why the edit function is not showing the selection made before.
I have followed the solution in:

It is the same array use in a config-file which is used in the add.ctp.
Store values are stored using an implode-function. The database shows correct data.

'Members' => [
    '0' => 'kein Stand',
    '1' => 'Mitglied',
    '2' => 'Interessent',
    '3' => 'Ehemaliger',
    '4' => 'Bewohner',
    '5' => 'Partner',
    '6' => 'Warteliste',
    '7' => 'Gärtner',
 ],

edit.ctp
$selMember = explode(’#’, $user->member);
/=======================

     <?php
       echo $this->Form->label('User.status', 'Status'); 
        $options = $statusList;
        echo $this->Form->radio('status', $options);           
        echo $this->Form->label('User.member', 'Mitglied'); 
        $options =  $memberList;            
         echo $this->Form->input('member', array('label'  => false,'type' => 'select',
                                                'multiple'=>'checkbox',
                                                'options' => $options,
                                                'selected' => $selMember)); 
        // echo $this->Form->select('member', $options, ['multiple' =>  'checkbox', 'selected' => $selMember] );

        echo $this->Form->input('userappartment_id', ['options' => $userappartments, 'label' => 'Wohnung']);
        echo $this->Form->input('userparking_id', ['options' => $userparkings, 'label' => 'Parkplatz']);
        echo $this->Form->input('contact', ['label' => 'Kontakt']);
        ?>

I’m pretty sure that “selected” should contain an array of keys instead of value; If I see it correctly, yours contains values like [“Interessent”, “Partner”] etc. while it should contain [2, 5] etc.

Hi ali, I agree with you and expect the same result… but

this is what print_r displays and I would expect the checkbox index 1 and index 4 to be checked. Nothing. The HTML :

Oh, apparently you need “default” instead of “selected”:

<?php $options = [
	0 => 'kein Stand',
	1 => 'Mitglied',
	2 => 'Interessent',
	3 => 'Ehemaliger',
	4 => 'Bewohner',
	5 => 'Partner',
	6 => 'Warteliste',
	7 => 'Gärtner',
];

$selMember = [
	2, 4
];

echo $this->Form->select('select', $options, [
	'label'    => false,
	'multiple' => true,
	'default'  => $selMember
]) ?>