Home  >  Article  >  php教程  >  yii去掉必填项中星号的方法,yii必填项中星号

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

WBOY
WBOYOriginal
2016-06-13 08:48:47848browse

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

本文实例讲述了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即可

不显示星号就可这样

<&#63;php CHtml::$afterRequiredLabel = '';&#63;>
<&#63;php echo $form->labelEx($model,'email'); &#63;>

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

您可能感兴趣的文章:

  • Yii入门教程之目录结构、入口文件及路由设置
  • Yii入门教程之Yii安装及hello world
  • Yii PHP Framework实用入门教程(详细介绍)
  • Yii查询生成器(Query Builder)用法实例教程
  • YII使用url组件美化管理的方法
  • Yii中CGridView实现批量删除的方法
  • yii权限控制的方法(三种方法)
  • yii数据库的查询方法
  • YiiFramework入门知识点总结(图文教程)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn