Search code examples
cakephpformhelper

Cake PHP 2 custom Form->Label


I'm using the Form helper to generate a label:

$this->Form->label('Contact.name', 'Name');

Which generates the following:

<label for="ContactName">Name</label>

Is it possible to generate the following using the helper:

<label for="ContactName"><span class="mandatory">*</span> Name</label>

Whilst I can manually write the html for the above it becomes a little more difficult when I am using the input method where a label is automatically generated.

For example:

$this->Form->input('Contact.forename',array('div' =>false,
                   'label' => array(
                   text'=> 'First Name',class =>'myclass'),
                   'class' => 'input','size' => '25' ,'tabindex' => '1'));

Is this possible in cake or do I have to manually inject the html using javascript when the page loads? Which I would think is rather ugly.


Solution

  • If you are using model validation for the mandatory fields then cakephp automatically applies '*' on the Label else you can use the helper as follows-

    echo $this->Form->label('name', '<span class="mandatory">*</span> Name');
    

    If you don't want the labels to generate automatically you can use "label => false" while using the helper.

    echo $this->Form->input('Contact.forename',array('label' =>false));