Cake PHP 2 custom Form-> Label

I use a form helper to create a label:

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

Which generates the following:

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

Is it possible to create the following using the assistant:

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

Although I can manually write html for the above, it gets a little harder when I use an input method where a label is automatically created.

For instance:

 $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 the cake or do I need to manually enter html using javascript when the page loads? I think this is pretty ugly.

+6
source share
4 answers

If you use model validation for required fields, then cakephp automatically applies "*" to the else label, you can use the helper as follows:

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

If you do not want the tags to be generated automatically, you can use "label => false" when using the helper.

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

Not sure if CakePHP supports this (and it will be a bit messy anyway). The simplest solution I can think of is to assign a label to the β€œrequired” class using the form helper:

 $this->Form->label('User.name', 'Your username', array('class'=>'mandatory')); 

Which produces something like:

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

Then the rest is done exclusively in CSS:

 label.mandatory:after { content: ' *'; color: red; display: inline; } 

Avoid the presence of additional HTML.

+4
source

I know this is old, but maybe someone with Cakephp 3 has the same problem. This is what fixed for me, without any inlinecode.

 <?php echo $this->Form->input( 'renovate_old', [ 'type' => 'checkbox', 'label' => ['text' => __('Alte Wohnung'), 'class' => 'moCheckLabel'] ]); ?> 

So you can name your tag and use the Databasefield field for writing.

+1
source

you can do it just

 echo $this->Form->input('whatever', array('between'=>'<label for="ContactName"><span class="mandatory">*</span> Name</label>','label'=>false)); 
0
source

Source: https://habr.com/ru/post/921812/


All Articles