Search code examples
cakephpformhelper

Cakephp 2.0: Change select item text with FormHelper without modifying ID


I'm sure this is simple but I can't figure out how to achieve it:

I have a model with a title and sub_title column; there are frequently entries that have the same title and are differentiable only by their sub_title. So, I need the views for CRUD commands to display effectively this:

<option>Title: Subtitle</option>

But I don't know how to get FormHelper to do this. Can someone provide a strategy to a generic form of this (ie. :

echo $this->Form->input('title', array(
                                        //whatever option(s) solve this 
                                       ));

Solutions or advice greatly appreciated!


Solution

  • Set your options differently, something using the Set class:

    $results = $this->Model->find('all');
    $options = Set::combine($results, '{n}.Model.id', array('{0}: {1}', '{n}.Model.title', '{n}.Model.sub_title'));
    

    So your option array would look something like

    array(
      0 => 'Title: sub title',
      1 => 'Title: different sub'
    );
    

    And set your dropdown to use those options

    $this->Form->input('title', array('options' => $options));