I've been using CakePHP to develop an application and I have some basic permissions that are aimed to restrict the ability of the users and help to prevent errors.
I have a form where someone with the a certain permission or higher can create a new member of staff. One of the fields in the form allows them to associate a school to a member of staff. If they have the minimum access required I would like selection to auto-fill as the school the creator has associated with them and then if they are an admin they have the option of choosing from the selection.
In the controller I am calling this line which populates the array of schools:
$this->set('schools', $this->Staff->School->find('list', array('conditions' => array('School.active' => !null), 'order' => array('name' => 'ASC'))));
And the array looks like this:
array(4) {
[2]=> string(15) "School A"
[3]=> string(15) "School B"
[1]=> string(17) "School C"
[6]=> string(21) "School D"
}
The AuthComponent stores the school id in the following way: AuthComponent::user('school_id') and for the purpose of this the value is set to 1.
In the add view the conditions for showing the selection look like this:
if (AuthComponent::user('admin') == 1) {
echo $this->Form->input('school_id', array('label' => 'School *', 'options' => array($schools), 'required' => 'required'));
} else {
echo $this->Form->input('school_id', array('label' => 'School *', 'value' => $schools[AuthComponent::user('school_id')], 'disabled' => 'disabled'));
}
For some reason the disabled view defaults to showing the first school in the list. I have used var_dump($schools[AuthComponent::user('school_id')]; which returns String(8) => "School C"so I am confused on why the selection will not show correctly.
Does anyone have any suggestions or an alternative solution to achieving the same outcome?