Hey! Total CakePHP noob here.
Updated at bottom \/
This is sort of a two fold question. In a view that is used for adding user
objects I would like to use a drop down (selection) field in the form.
Each user
belongs to a group
so when I add a user I want a drop down that contains all of the groups that the user could possibly join. Currently the group_id
field is a textfield. I know how to force it to be a selection field, but I don't know how to populate the selection with the names of the groups programmatically.
The Current method:
echo $form->input('group_id', array(
'1' => 'NameOfGroup1',
'2' => 'NameOfGroup2',
'3' => 'NameOfGroup3')
);
I want to generate the options array programmatically though.
echo $form->input('group_id', $this->Group->find('list'));
This doesn't work though. I get an error:
Undefined property: View::$Group [APP/views/users/add.ctp, line 8]
To me this means that I don't have access to the Group
object from inside my user view.
How can I accomplish this? Again, I want to do it programmatically so that it updates as I add groups or remove them.
EDIT: Why doesn't this work?
// In views/users/someaction.ctp
echo $form->input('group_id',
array('options' => $this->formOptionsGroups)
);
// In controllers/users_controller.php
function someaction() {
// Any other logic
$this->set('formOptionsGroups', $this->Group->find('list'));
}
Error is that Group is an undefined object.