Devi pensare in modo dissociato le cose. L’asterisco appare perché le lable sono campi obbligatori. Se analizzi bene il codice, scopri che quando un campo è required il campo label ha la classe css “required”, altrimenti no. Partendo da questo assunto, quello di cui hai bisogno, è l’asterisco in caso di campo obbligatorio, e la scritta (opzionale) in tutti gli altri casi.
Per risolvere questo problema, dobbiamo fare un hack di CHtml. In particolare CHtml::label();
public static function label($label,$for,$htmlOptions=array())
{
if($for===false)
unset($htmlOptions['for']);
else
$htmlOptions['for']=$for;
if(isset($htmlOptions['required']))
{
if($htmlOptions['required'])
{
if(isset($htmlOptions['class']))
$htmlOptions['class'].=' '.self::$requiredCss;
else
$htmlOptions['class']=self::$requiredCss;
$label=self::$beforeRequiredLabel.$label.self::$afterRequiredLabel;
}
unset($htmlOptions['required']);
}
return self::tag('label',$htmlOptions,$label);
}
In particolare questo pezzetto:
if($htmlOptions['required'])
{
if(isset($htmlOptions['class']))
$htmlOptions['class'].=' '.self::$requiredCss;
else
$htmlOptions['class']=self::$requiredCss;
$label=self::$beforeRequiredLabel.$label.self::$afterRequiredLabel;
}
unset($htmlOptions['required']);
Qui si dice che se è stato impostato l’htmlOption ‘required’ deve essere messa, o aggiunta la classe self::$requiredCss (che poi è una stringa ‘required’). A naso, io proverei a modificare quel codice (CHtml::label(). Mi pare di aver letto da qualche parte che non è possibile sovrascrivere CHtml, quindi temo tu sia costretto a modificare la classe del framework.
if($htmlOptions['required'])
{
if(isset($htmlOptions['class']))
$htmlOptions['class'].=' '.self::$requiredCss;
else
$htmlOptions['class']=self::$requiredCss;
$label=self::$beforeRequiredLabel.$label.self::$afterRequiredLabel;
} else {
if(isset($htmlOptions['class']))
$htmlOptions['class'].=' '.self::$optionalCss;
else
$htmlOptions['class']=self::$optionalCss;
$label=self::$beforeOptionalLabel.$label.self::$afterOptionalLabel;
}
unset($htmlOptions['required']);
Quindi dovresti creare la proprietà $optionalCss. Questa è quella di required
/**
* @var string the CSS class for required labels. Defaults to 'required'.
* @see label
*/
public static $requiredCss='required';
e questo è quello che potresti fare
/**
* @var string the CSS class for required labels. Defaults to 'required'.
* @see label
*/
public static $requiredCss='required';
/**
* @var string the CSS class for optional labels. Defaults to 'optional'.
* @see label
*/
public static $optionalCss='optional';
A questo punto abbiamo il css optional, però non abbiamo il css optional.
Questo è il css dentro al file form.css:
div.form span.required
{
color: red;
}
Il css non è necessario, ma per coerenza, io lo metterei:
div.form span.required
{
color: red;
}
div.form span.optional
{
}
Potrebbe venirti in mente di cambiare il css di tutti i campi opzionali. Ma non è finita qui:
Chi ce lo mette quel dannatissimo *? Si vede bene nel ramo dell’else.
} else {
if(isset($htmlOptions['class']))
$htmlOptions['class'].=' '.self::$optionalCss;
else
$htmlOptions['class']=self::$optionalCss;
$label=self::$beforeOptionalLabel.$label.self::$afterOptionalLabel;
}
Ovvero qui:
$label=self::$beforeOptionalLabel.$label.self::$afterOptionalLabel;
Questo è il css del required, noi dovremmo creare quello dell’optional
/**
* @var string the HTML code to be appended to the required label.
* @see label
*/
public static $afterRequiredLabel=' <span class="required">*</span>';
… così:
/**
* @var string the HTML code to be appended to the optional label.
* @see label
*/
public static $afterOptionalLabel=' <span class="optional">(optional)</span>';
/**
* @var string the HTML code to be appended to the required label.
* @see label
*/
public static $afterRequiredLabel=' <span class="required">*</span>';
Ecco risolto l’arcano.
Non credo sia la soluzione migliore. Però è efficace. Te la sconsiglio vivamente: ti obbliga a modificare il framework.