Adding “empty” to list-find, for the lazy
Currently i use the scaffold views alot to bake a simple admin interface. What annoyed me was that ‘empty’ is not set on select inputs. I could of course go through all add/edit views and add it, but i thought i rather modify the result to include a empty entry.
Always add a “empty” entry to the result
function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
if (is_array($conditions)) {
return parent::find($conditions, $fields, $order, $recursive); // old interface
} else {
switch ($conditions) {
case 'list':
$result = parent::find($conditions, $fields, $order, $recursive);
$return = $result; // default
if (!empty($result)) {
$return = aa('', __("-- please select --", true));
foreach ($result as $id => $name)
$return[$id] = $name;
}
return $return;
break;
default:
return parent::find($conditions, $fields, $order, $recursive);
break;
}
}
}
Okay, that works and i don’t have to add ‘empty’ everywhere. I modified the method a little further (not actually using it, but maybe someone likes it..) for you to grab. It will still require that you modify all files, but this way you have more control.
Adding “empty” to the find() options array
function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
if (is_array($conditions)) {
return parent::find($conditions, $fields, $order, $recursive); // old interface
} else {
switch ($conditions) {
case 'list':
$result = parent::find($conditions, $fields, $order, $recursive);
$return = $result; // default
if (!empty($result) && isset($fields['empty']) && $fields['empty'] != false) {
$return = aa('', $fields['empty']); // begin with whatever is in 'empty'
foreach ($result as $id => $name)
$return[$id] = $name;
}
return $return;
break;
default:
return parent::find($conditions, $fields, $order, $recursive);
break;
}
}
}
Everything above goes to app_model.php of course. With the last snippet you are able to move the “empty” option to the controller and go like this:
$categories = $this->Category->find('list', array('empty' => __("-- please select --", true)));
Enjoy & Happy Easter!


Theoretically, wouldn’t you rather throw something in your app_helper.php to accomplish that? I believe this belongs to the View. Sorry, as I’m too lazy, no code right now :)
But handy anyway, thanks.
FormHelper extends AppHelper – so that wouldn’t really work. I could either overwrite the form.php (bad) or extend FormHelper in order to overwrite input/select (good). The latter would require to add and use a custom form helper, which also means i need to rewrite all views. A task i tried to avoid. So no gain from doing so.
I think the above the only way to go if you don’t want to touch the baked form views at all.
What about:
:) – the “<?php” and “ ” aside – for snippets use “<pre>” to preserve indention and stuff..
And if you want to make it fancy use <pre class=”sh_php”>.
how about adding
array(’empty’ => ”) on the options param of $form->input(’field’, $param);
?