How to remove the asterisk in required fields?
Let’s first analyze the code implementation:
public function labelEx($model,$attribute,$htmlOptions=array())
{
return CHtml::activeLabelEx($model,$attribute,$htmlOptions);
}
public static function activeLabelEx($model,$attribute,$htmlOptions=array())
{
$realAttribute=$attribute;
self::resolveName($model,$attribute); // strip off square brackets if any
$htmlOptions['required']=$model->isAttributeRequired($attribute);
return self::activeLabel($model,$realAttribute,$htmlOptions);
}
When the attribute is required, it will render additional CSS class tags. Specifically, it calls CModel::isAttributeRequired to determine whether the attribute is required. If so, it will add a CSS class CHtml::requiredCss (public static $requiredCss='required';) to the label, with CHtml::beforeRequiredLabel (public static $beforeRequiredLabel='';) and CHtml::afterRequiredLabel (public static $afterRequiredLabel='*';) to decorate the label.
public function isAttributeRequired($attribute)www.2cto.com
{
foreach($this->getValidators($attribute) as $validator)
{
if($validator instanceof CRequiredValidator) return true;
}
return false;
}
So if you want to remove the asterisk or change it to something else, you can directly redefine CHtml::requiredCss, CHtml::beforeRequiredLabel, and CHtml::afterRequiredLabel in the view
Just do this without showing the asterisk
labelEx($model,'email'); ?>
http://www.bkjia.com/PHPjc/477799.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477799.htmlTechArticleHow to remove the asterisk in required fields? Let’s first analyze the code implementation: public function labelEx($model,$attribute,$htmlOptions=array()) { return CHtml::activeLabelEx($model,$attribute,$...