CHtml::label, CHtml::activeTextBox and determining IDs

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') ?>


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') ?>


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?

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',



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.