当前位置: 移动技术网 > IT编程>开发语言>PHP > yii去掉必填项中星号的方法

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

2018年04月20日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网