本文实例讲述了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程序设计有所帮助。
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 60
-
2025-05-25 46
-
2025-05-25 84
-
2025-05-24 47
-
asp.net core利用AccessControlHelper实现控制访问权限
2025-05-29 57
热门评论