Two lines of code in Yii.
How to surround a checkbox with a <label> tag and display your text? (now 'Only Crash' is displayed in the label) ???

<div class="form"> <?php $form = $this->beginWidget('CActiveForm'); ?> <?php print $form->errorSummary($eventDateForm); ?> <div class="row"> <label>Укажите временной интервал</label> <label> С <?php print $form->textField($eventDateForm, 'eventPeriodBegin', array('value' => date("dmY"), 'class' => 'span1')); ?> По <?php print $form->textField($eventDateForm, 'eventPeriodEnd', array('value' => date("dmY"), 'class' => 'span1')); ?> </label> <?php print $form->checkBox($eventDateForm, 'onlyCrash', array('value' => TRUE, 'class' => 'span1'))?> <?php print $form->label($eventDateForm, 'onlyCrash'); ?> </div> <div class="row submit"> <?php echo CHtml::submitButton('Показать события на объекте'); ?> </div> <?php $this->endWidget(); ?> </div><!-- form --> 

In the manual did not find.

UPDATE all widget code above

  • Why surround the checkbox with a label tag? Which package is used, CHtml or CActiveForm? - Zhukov Roman
  • beauty sake of. Literally: I want the checkbox and its description to be on the same line. - zenith
  • Well, this is solved with CSS. A description of the fields CActiveForm takes from the model. - Zhukov Roman
  • @Zhukov Roman Disagree with you. Label text is taken from the second parameter. Code: <? Php print $ form-> label ($ eventDateForm, 'onlyCrash'); ?> spits out html: <label for = "EventForm_onlyCrashText"> Only Crash </ label> Similarly: <? php print $ form-> label ($ eventDateForm, 'stupid text'); ?> It will give out: <label for = "EventForm_topo_text"> topo text </ label> I need the label to have the for attribute on my checkbox and the text is different, not the name. - zenith
  • I dealt with the text myself: <? Php print $ form-> label ($ eventDateForm, 'my text', array ('for' => 'onlyCrash')); ?> Specify for in the list of additional options. <label for = "onlyCrash"> my text </ label> It now remains to figure out how to surround my checkbox with this label now - zenith

2 answers 2

 <div class="row"> <?php echo $form->labelEx($model,'points_id'); ?> <?php echo $form->checkboxlist($model, 'points_id', points::model()->getPoints());?> <?php echo $form->error($model,'points_id'); ?> </div> 

You just do diva, although it is always in its basic form, and you take the label. and Roman correctly noted what you use. Everything depends on it.

  • Well, judging by the parameters, he has CActiveForm. I just did not touch Yii for a long time, forgot it. - Zhukov Roman
  • and what is the actual problem, add the tag <label> ?? - Artem
  • @Shrek only in what I want through api :) - zenith
  • I have already written an example in response. - Artem
  • There is no getPoints () method in manus. What does it do? - zenith

Do you mean a template?

  echo CHtml::checkBox('im_id2', array(), array( 'template' => '{input} {labelTitle}', ));