Wednesday, June 1, 2011

What's the best way to change select box option labels when using Automagic forms?

I have a standard baked controller with add and edits for a list of
students. Each student belongs to one family, so I have this in my
Student Model:

var $belongsTo = array('Family' => array(
'className' => 'Family',
'foreignKey' => 'family_id')
);

In my student view, the Family select box (echo $this->Form-
>select('family_id');) only shows the family_id (INT) as both the
value and the label. What I'd like to do is instead, keep the value as
the family_id, but the label should be last_name, first_name. So I
wrote this in my students_controller (not sure if it's the best
approach):
$families = $this->Student->Family->find(
'all',
array(
'fields' => array(
'last_name',
'first_name'
),
'order' => array(
'Family.last_name ASC'
)
)
);
foreach($families as $row)
{
$families_list["{$row['Family']['id']}"] = "{$row['Family']
['last_name']}, {$row['Family']['first_name']}";
}
$this->set('families_list', $families_list);

Then in the view:

echo $this->Form->select('family_id', $families_list);

This works for my add view, but for my edit view, the select box is
empty.

Any ideas on how to get this to work for my edit view while still
maintaining the selected value?

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: