Yii Framework Forum: CHtml::label, CHtml::activeTextBox and determining IDs - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

CHtml::label, CHtml::activeTextBox and determining IDs Rate Topic: -----

#1 User is offline   ShabbyRobe 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 13-October 08

Posted 15 October 2008 - 11:38 PM

I've hit a bit of an impasse with something - I'm trying to assign my own text for a label in a form and I had a look through the API for something, settling on generateAttributeLabel and getAttributeLabel before realising I was doing the renderDynamicInternal thing - looking for a programmatic solution for something which can be solved just by manually writing a <label> tag into the view, or using CHtml::label instead of CHtml::activeLabel.

The problem is, I can't set the 'for' property, because I don't know what the ID of the activeTextBox is going to be until after I've created it, and CHtml::resolveNameId is protected so I can't use CHtml's method of creating the ID.


Using activeLabel, which gives me a label I don't want (Artistname):
<div class="simple">
<?php echo CHtml::activeLabel($artist,'artistName'); ?>
<?php echo CHtml::activeTextField($artist,'artistName') ?>
</div>

Using label, which means I can't set the 'for' attribute:
<div class="simple">
<?php echo CHtml::label('Artist Name', '????); ?>
<?php echo CHtml::activeTextField($artist,'artistName') ?>
</div>


Should I go back to hacking around on getAttributeLabel? It feels like it would be neater if I could just get the ID of the text field somehow.

Also, would it be worth modifying generateAttributeLabel to deal with camel casing with something like this:

return trim(ucwords(strtolower(preg_replace('/[A-Z_]/', ' ', $name))));

or would the use of a regexp be unacceptable from a performance perspective?
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,900
  • Joined: 04-October 08
  • Location:DC, USA

Posted 16 October 2008 - 05:52 AM

I just changed generateAttributeLabel according to your suggestion, and added CHtml::getActiveId().

If you have more uncommon label generation, you should override the attributeLabels() method of your model class, for example:

public function attributeLabels()
{
        return array(
              'artistName'=>'My Artist Name',
        );
}


0

#3 User is offline   ShabbyRobe 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 13-October 08

Posted 16 October 2008 - 07:48 AM

attributeLabels() is exactly what I need. Thanks muchly for the changes. getActiveId() will also come in handy for scenarios where I want to customise the label for different views.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users