I have a belongstomany association in two tables (Users and Groups, intermediate table UsersGroups) and I need to allow a multiple select or a single select on add view depending on profile_id.
This is the code for add action in UsersGroups controller:
public function add()
{
$usersGroup = $this->UsersGroups->newEmptyEntity();
if ($this->request->is('ajax')) {
$user_id = $this->request->getData('user_id');
$user = $this->UsersGroups->Users->get($user_id);
$campo = ($user->profile_id==3) ? 'groups._ids' : 'group_id';
$this->set('campo', $campo);
}elseif ($this->request->is('post')) {
$usersGroup = $this->UsersGroups->patchEntity($usersGroup, $this->request->getData());
if ($this->UsersGroups->save($usersGroup)) {
$this->Flash->success(__('The users group has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The users group could not be saved. Please, try again.'));
}
$users = $this->UsersGroups->Users->find('list', ['limit' => 200])->all();
$nets = $this->UsersGroups->Nets->find('list', ['limit' => 200])->all();
$groups = $this->UsersGroups->Groups->find('list', ['limit' => 200])->all();
$this->set(compact('usersGroup', 'users', 'nets', 'groups'));
}
And this is the code for add view:
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\UsersGroup $usersGroup
* @var \Cake\Collection\CollectionInterface|string[] $users
* @var \Cake\Collection\CollectionInterface|string[] $nets
* @var \Cake\Collection\CollectionInterface|string[] $groups
*/
?>
<div class="row">
<aside class="column">
<div class="side-nav">
<h4 class="heading"><?= __('Actions') ?></h4>
<?= $this->Html->link(__('List Users Groups'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
</div>
</aside>
<div class="column-responsive column-80">
<div class="usersGroups form content">
<?= $this->Form->create($usersGroup) ?>
<fieldset>
<legend><?= __('Add Users Group') ?></legend>
<?php
echo $this->Form->control('user_id', ['options' => $users, 'id' => 'userId']);
if(!empty($campo)){
echo $this->Form->control($campo, ['options' => $groups, 'id' => 'groupsId']);
}
echo '<div id="selectNet" style="display:none;">'.$this->Form->control('net_id', ['options' => $nets]).'</div>';
echo '<div id="selectGate" style="display:none;">'.$this->Form->control('main_gate', ['options' => ['A' => 'A', 'B' => 'B']]).'</div>';
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
</div>
</div>
<script>
$(function(){
$('#userId').change(function(){
$.ajax({
method:"POST",
url:"<?= $this->Url->build(['controller' => 'UsersGroups', 'action' => 'add']) ?>",
data:{
user_id:$(this).val()
},
success: function(data) {
<?php
if(!empty($profile_id)) {
switch ($profile_id) {
case "4": echo "$('#selectGroup').show();$('#selectNet').show();$('#selectGate').hide();";
break;
case "5": echo "$('#selectGroup').show();$('#selectNet').show();$('#selectGate').show();";
break;
case "6": echo "$('#selectGroup').show();$('#selectNet').show();$('#selectGate').show();";
break;
case "7": echo "$('#selectGroup').show();$('#selectNet').show();$('#selectGate').show();";
break;
}
}
?>
},
headers:{
'X-CSRF-Token':$('meta[name="csrfToken"]').attr('content')
}
})
})
})
</script>
How can I solve this issue?