php - checkbox not checked while editing the user record in cakephp -
i have table user
have fields username
, password
, , type
. type
can or combination of these: employee, vendor , client e.g. user can both vendor , client, or combination. type
field have used multiple checkbox (see code below). 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 model file have used 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 multiple values comma separated values in database.
the main problem comes when i'm editing user. if user has selected multiple types during user creation, none of checkboxes checked type.
you have $this->data['user']['type'] = join(',', $this->data['user']['type']);
in beforesave() need same in afterfind() $this->data['user']['type'] = explode(',', $user['user']['type']);
make array again.
this horrible design, should consider doing properly.
Comments
Post a Comment