I have table user
which have fields username
, password
, and type
. The type
can be any or a combination of these: employee, vendor and client e.g. a user can be both a vendor and a client, or some another combination. For the type
field I have used the multiple checkbox (see the code below). This is the views/users/add.ctp file:
<div class="users form">
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php __('Add User'); ?></legend>
<?php
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('type', array('type' => 'select', 'multiple' => 'checkbox','options' => array(
'client' => 'Client',
'vendor' => 'Vendor',
'employee' => 'Employee'
)
));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
In the model file I have used a beforeSave
callback method:
app/models/user.php
function beforeSave() {
if(!empty($this->data['User']['type'])) {
$this->data['User']['type'] = join(',', $this->data['User']['type']);
}
return true;
}
This code saves the multiple values as comma separated values in the database.
The main problem comes when I'm editing a user. If a user has selected multiple types during user creation, none of the checkboxes is checked for the type.