Home  >  Article  >  Backend Development  >  How to remove asterisks in required fields in yii, asterisks in required fields in yii_PHP tutorial

How to remove asterisks in required fields in yii, asterisks in required fields in yii_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:02:21847browse

yii's method of removing asterisks in required items, yii's asterisks in required items

This article describes the example of yii's method of removing asterisks in required items. Share it with everyone for your reference, the details are as follows:

How to remove the asterisks in 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, and CHtml::afterRequiredLabel in the view

Just do this without showing the asterisk

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

I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.

Articles you may be interested in:

  • Yii Getting Started Tutorial: Directory Structure, Entry File and Routing Settings
  • Yii Getting Started Tutorial: Yii Installation and Hello World
  • Yii PHP Framework practical introductory tutorial (detailed introduction)
  • Yii Query Builder (Query Builder) usage example tutorial
  • YII method of using url component to beautify management
  • Yii Methods to implement batch deletion in CGridView
  • Yii permission control methods (three methods)
  • Yii database query methods
  • Summary of YiiFramework entry-level knowledge points (graphic tutorial)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1085881.htmlTechArticleyii method to remove asterisks in required fields, the asterisks in yii required fields are explained in this article. Methods for asterisks in required fields. Share it with everyone for your reference, the details are as follows: How to remove...
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