How to remove asterisks in required fields in yii

WBOY
Release: 2016-07-29 09:08:39
Original
1294 people have browsed it

The example in this article describes how Yii removes asterisks in required fields. Share it with everyone for your reference, the details are as follows:

How to remove the asterisks in the required fields?

First analyze the code implementation:

public function labelEx($model,$attribute,$htmlOpti
{
  return CHtml::activeLabelEx($model,$attribute,$htmlOptions);
}
public static function activeLabelEx($model,$attribute,$htmlOpti
{
  $realAttribute=$attribute;
  self::resolveName($model,$attribute); // strip off square brackets if any
  $htmlOptions['required']=$model->isAttributeRequired($attribute);
  return self::activeLabel($model,$realAttribute,$htmlOptions);
}

Copy after login

When the attribute is required, it will render additional CSS class tags. Specifically, it calls CModel::isAttributeRequired to determine whether the attribute is required. If so, it will add a CSS class CHtml::requiredCss (public static $requiredCss='required';) to the label, with CHtml::beforeRequiredLabel (public static $beforeRequiredLabel='';) and CHtml::afterRequiredLabel (public static $afterRequiredLabel='*';) to decorate the label.

public function isAttributeRequired($attribute)
{
  foreach($this->getValidators($attribute) as $validator)
  {
    if($validator instanceof CRequiredValidator) return true;
  }
  return false;
}

Copy after login

So if you want to remove the asterisk or change it to something else, you can directly redefine CHtml::requiredCss, CHtml::beforeRequiredLabel, CHtml::afterRequiredLabel in the view

Just don’t show the asterisk

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

Copy after login

I hope this article will help you design PHP programs based on the Yii framework.

The above introduces the method of removing asterisks in required fields in Yii, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!