Changes
Title
unchanged
Extending CActiveForm for some form display fixes and language tweaks
Category
unchanged
Tips
Yii version
unchanged
Tags
unchanged
extending, CActiveForm, form, fixes, twaks
Content
changed
[...]
return $label_html.$brl.$label_text.':'.$arl.'</label>';
}
else return str_replace('</label>', ':</label>', CHtml::activeLabelEx($model, $attribute, $htmlOptions));
}
```
### Asterisk NOT required, but colon on the contrary...
In addition to above function, I wrote another one that removes everything related to field being required (i.e. asterisk and CSS styling) leaving colon in place. Removing required attributes from label is a build-in feature of form generator, you just need to change _labelEx_ function from your view to simple _label_ one. But then, again, you also loose colon. For those that need it at the end of label, but not the required attributes, here is a function for this purpose:
```php
public function labelNA($model, $attribute, $htmlOptions = array())
{
if ($model->isAttributeRequired($attribute))
{
$arl = CHtml::$afterRequiredLabel;
$brl = CHtml::$beforeRequiredLabel;
$label = CHtml::activeLabelEx($model, $attribute, $htmlOptions);
$label = str_replace($arl, '', $label);
$label = str_replace($brl, '', $label);
$label = str_replace(' class="required"', '', $label);
$label_text = strip_tags($label);
$label_html = str_replace($label_text, '', $label);
$label_html = str_replace('</label>', '', $label_html);
return $label_html.$label_text.':</label>';
}
else return str_replace('</label>', ':</label>', CHtml::activeLabelEx($model, $attribute, $htmlOptions));
}
```
### Textual information of maximum number of characters
And here is overloaded version of CActiveForm.textField that adds simple span after input box with information on how many characters may be entered to it, with respecting plural form of number endings:
```php
const TEXT_FIELD_EX_BEGIN = '(enter up to {size} ';
const TEXT_FIELD_EX_EXPR = 'n==1#char|n>=1#chars';
const TEXT_FIELD_EX_END = ')';
public function textFieldEx($model, $attribute, $htmlOptions = array())
{
if(isset($htmlOptions['maxlength']))[...]
$max = $htmlOptions['maxlength'];
$transl = Yii::t('classes', 'MyActiveForm_textFieldEx_begin', array('{size}'=>$max));
$transl.= CChoiceFormat::format(Yii::t('classes', 'MyActiveForm_textFieldEx_expr'), $max);
$transl.= Yii::t('classes', 'MyActiveForm_textFieldEx_end')self::TEXT_FIELD_EX_BEGIN.$max.' ';
$transl.= CChoiceFormat::format(self::TEXT_FIELD_EX_EXPR, $max);
$transl.= self::TEXT_FIELD_EX_END;
$textFieldAdd = ' <span class="text_field_add">'.$transl.'</span>';
}
return CHtml::active
PasswordTextField($model, $attribute, $htmlOptions).$textFieldAdd;
}
```
Th
is utilizes my own approach to Yii::t() where second parameter isn't English text (as in yii.php and zii.php files in original framework) but actually a placeholder, label or pattern to be exchanged. Therefore you have to create your own classes.php and put there something similar to:
```php
return array
(
'MyActiveForm_textFieldEx_begin' => '(enter up to {size} ',
'MyActiveForm_textFieldEx_expr' => 'n==1#char|n>1#chars',
'MyActiveForm_textFieldEx_end' => ')',
);
```e above example might not be perfect as grammar in English is quite easy for doing plural form (but not that easy, look into [CCodeModel::pluralize()](http://www.yiiframework.com/doc/api/1.1/CCodeModel/#pluralize-detail "CCodeModel::pluralize()"). But in other languages (for example in my native Polish), it is so complicated, that even regular expressions can't handle it! :)
### Final words
I understand that
the above code is far from perfect. It was written very quickly, so everyone please feel free to edit and correct it.