本文实例讲述了yii去掉必填项中星号的方法。分享给大家供大家参考,具体如下:
如何去掉必填项里的星号呢?
先分析下代码实现:
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);
}
当属性是必填的时候,它将渲染额外的css类个标记。特别的,它调用cmodel::isattributerequired来决定属性是否为必填的。如果是,它将添加一个css类chtml::requiredcss (public static $requiredcss='required';)到标签上,用chtml::beforerequiredlabel(public static $beforerequiredlabel='';)和chtml::afterrequiredlabel (public static $afterrequiredlabel='*';)来装饰标签。
public function isattributerequired($attribute)
{
foreach($this->getvalidators($attribute) as $validator)
{
if($validator instanceof crequiredvalidator) return true;
}
return false;
}
所以要去掉星号 或者换成别的可以再view中直接重新定义chtml::requiredcss、chtml::beforerequiredlabel、chtml::afterrequiredlabel即可
不显示星号就可这样
<?php chtml::$afterrequiredlabel = '';?> <?php echo $form->labelex($model,'email'); ?>
希望本文所述对大家基于yii框架的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!