Is there a way to "automagically" add the empty option to a drop down list?
What I'm looking for is to add this option on a drop down list of a belongTo relation that can be null, without having to add code on the view.
I do believe this should be a Model option, that could be useful for example on a scaffold.
You could override the _findList
method or make a new custom find and use _findList (call it say _findSelectList
). Example:
protected function _findList($state, $query, $results = array()) {
$return = parent::_findList($state, $query, $results);
if ($state === 'after') {
$return = array('' => 'select one') + $return;
}
return $return;
}
Or you could extend FormHelper and add the empty option there by default which I think would be simpler. You may actually be able to get away with setting empty
in inputDefaults
when calling $this->Form->create('Model', array('inputDefaults' => array('empty' => 'Select One')));
. If this works and you don't want to do it in every form, extend FormHelper and set this as the default in it.