TbHtml::error PHP Method

error() public static method

Displays the first validation error for a model attribute.
public static error ( CModel $model, string $attribute, array $htmlOptions = [] ) : string
$model CModel the data model.
$attribute string the attribute name.
$htmlOptions array additional HTML attributes.
return string the rendered error. Empty if no errors are found.
    public static function error($model, $attribute, $htmlOptions = array())
    {
        parent::resolveName($model, $attribute);
        // turn [a][b]attr into attr
        $error = $model->getError($attribute);
        $htmlOptions['type'] = self::HELP_TYPE_INLINE;
        return !empty($error) ? self::help($error, $htmlOptions) : '';
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Displays the first validation error for a model attribute.
  * @param CModel $model the data model
  * @param string $attribute the attribute name
  * @param array $htmlOptions additional HTML attributes to be rendered in the container div tag.
  * @param boolean $enableAjaxValidation whether to enable AJAX validation for the specified attribute.
  * @param boolean $enableClientValidation whether to enable client-side validation for the specified attribute.
  * @return string the validation result (error display or success message).
  */
 public function error($model, $attribute, $htmlOptions = array(), $enableAjaxValidation = true, $enableClientValidation = true)
 {
     if (!$this->enableAjaxValidation) {
         $enableAjaxValidation = false;
     }
     if (!$this->enableClientValidation) {
         $enableClientValidation = false;
     }
     if (!$enableAjaxValidation && !$enableClientValidation) {
         return TbHtml::error($model, $attribute, $htmlOptions);
     }
     $id = CHtml::activeId($model, $attribute);
     $inputID = TbArray::getValue('inputID', $htmlOptions, $id);
     unset($htmlOptions['inputID']);
     TbArray::defaultValue('id', $inputID . '_em_', $htmlOptions);
     $option = array('id' => $id, 'inputID' => $inputID, 'errorID' => $htmlOptions['id'], 'model' => get_class($model), 'name' => $attribute, 'enableAjaxValidation' => $enableAjaxValidation, 'inputContainer' => 'div.control-group');
     $optionNames = array('validationDelay', 'validateOnChange', 'validateOnType', 'hideErrorMessage', 'inputContainer', 'errorCssClass', 'successCssClass', 'validatingCssClass', 'beforeValidateAttribute', 'afterValidateAttribute');
     foreach ($optionNames as $name) {
         if (isset($htmlOptions[$name])) {
             $option[$name] = TbArray::popValue($name, $htmlOptions);
         }
     }
     if ($model instanceof CActiveRecord && !$model->isNewRecord) {
         $option['status'] = 1;
     }
     if ($enableClientValidation) {
         $validators = TbArray::getValue('clientValidation', $htmlOptions, array());
         $attributeName = $attribute;
         if (($pos = strrpos($attribute, ']')) !== false && $pos !== strlen($attribute) - 1) {
             $attributeName = substr($attribute, $pos + 1);
         }
         foreach ($model->getValidators($attributeName) as $validator) {
             if ($validator->enableClientValidation) {
                 if (($js = $validator->clientValidateAttribute($model, $attributeName)) != '') {
                     $validators[] = $js;
                 }
             }
         }
         if ($validators !== array()) {
             $option['clientValidation'] = "js:function(value, messages, attribute) {\n" . implode("\n", $validators) . "\n}";
         }
     }
     $html = TbHtml::error($model, $attribute, $htmlOptions);
     if ($html === '') {
         $htmlOptions['type'] = $this->helpType;
         TbHtml::addCssStyle('display:none', $htmlOptions);
         $html = TbHtml::help('', $htmlOptions);
     }
     $this->attributes[$inputID] = $option;
     return $html;
 }
All Usage Examples Of TbHtml::error
TbHtml