yii去掉必填项中星号的方法

2025-05-29 0 85

本文实例讲述了yii去掉必填项星号的方法。分享给大家供大家参考,具体如下:

如何去掉必填项里的星号呢?

先分析下代码实现:

?

1

2

3

4

5

6

7

8

9

10

11
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='*';)来装饰标签。

?

1

2

3

4

5

6

7

8
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即可

不显示星号就可这样

?

1

2
<?php CHtml::$afterRequiredLabel = '';?>

<?php echo $form->labelEx($model,'email'); ?>

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 yii去掉必填项中星号的方法 https://www.kuaiidc.com/99595.html

相关文章

发表评论
暂无评论