开发者

Change individual labels with automagic form helper on a 'multiple' => 'checkbox'

I have the following line in my add/edit Course views:

echo $this->Form->input(
    'Com开发者_如何学Pythonpetency', 
    array(
        'label' => 'Which competencies does this course address?', 
        'multiple' => 'checkbox'
    )
);

(Modelled as Course hasMany Competencies)

I'd like to customise the label that gets output for each competency but can't seem to find a way of doing so - the 'label' field seems to work as a group heading rather than changing the label for the individual checkboxes.

(What I'm ideally after is rather than just displaying Competency.name I can display Competency.name plus the Competency.code as the label)

NB I thought about changing the displayField but that would change it everywhere and it's only here I'd like to be different.


Create a virtual field [details]:

//in your Competency model
var $virtualFields = array(
    'name_code' => 'CONCAT(Competency.name, " ", Competency.code)'
);

Then in your controller, before retrieving the data, set your displayField to your just-created virtual field:

//in your controller prior to the find
$this->Competency->displayField = 'name_code';

Since you're setting displayField in the controller, it doesn't set it permanently, so no need to set it back, but if you're doing more finds immediately after this, you can always set back to name if you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜