
With this post I'm introducing a new segment to this blog: Cute CakePHP Trick of the Day.
This is basically going to be a learn as I learn sort of thing. There are always a bunch of little things that I want to do in CakePHP, but just haven't figured out. So every time I stumble onto something, I'll let you guys in on it.
I love the GenerateList() function. This is generally used when you have models with associations with other models. The GenerateList function is often used in these cases to populate a drop down list or a multiple select list. A typical example is a state drop down on an address form.
One problem that I have with this implementation is that it always produces a complete list and there is no empty slot. There is nothing to say that I don't want to associate anything for this entry. On a drop down list, it's impossible not to select an element. On a multiple selection list, even if the user holds CTRL and clicks to unselect the current entry, CakePHP ignores this entry.
Here's the fix. Assuming in your controller you have something like:
-
$this->;State->generateList();
and in the view:
Simply modify the view to give:
This will give you an entry up at the top that the user can select, which signifies empty with the text "--". Change the text to anything you like and you'll have the desired effect.
Popularity: 40%
If you're new here, you may want to subscribe to my Full RSS feed. Thanks for visiting!

It’s quite old trick. In fact there is not explanation how to do it.
I figured this through reading the code of the Form helper.
Nik Chankov, on November 1st, 2007 at 11:07 am
I can’t remember where I found it. I baked a couple of views and I was wondering how to do it. And as with everything else I’ve found I cake, I kept thinking: “This can’t be that hard”…
And it wasn’t, just hidden
Baz L, on November 1st, 2007 at 9:34 pm
Trackbacks & Pingbacks
CakePHP select ???????????????????? | Sun Limited Mt., on March 31st, 2008 at 1:52 am
CakePHP select ???????????????????? | Sun Limited Mt., on March 31st, 2008 at 7:16 pm
Leave a Reply (I Follow)
Most Popular Posts
Popular Commentors
Featured Sites
Tips and Tricks to Save Money
Tags
Add new tag Ajax apache auth backup bake bakery Blog browser CakePHP calendar cookie cron database date domain Fast CGI firefox form free ie internet explorer JavaScript Linux MySQL PHP ruby on rails School time tutorial wamp wordpressCopyright © Web Development 2.0. All rights reserved. Monitored by Uptime Dog