Home  >  Article  >  Backend Development  >  How to remove asterisks in required fields using yii

How to remove asterisks in required fields using yii

不言
不言Original
2018-06-15 14:25:331574browse

This article mainly introduces the method of removing asterisks in required items in Yii. The example analyzes the principle and specific implementation skills of removing asterisks in required items in Yii. Friends in need can refer to the following

The example in this article describes Yii's method of removing 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?

Let’s first analyze the code implementation:

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);
}

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;
}

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

Just don’t display the asterisk

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

##The above is the entire content of this article, I hope it will be helpful to everyone Learning will be helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How YII uses url components to beautify management

About the analysis of YII related queries

The above is the detailed content of How to remove asterisks in required fields using yii. For more information, please follow other related articles on the PHP Chinese website!

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