yii\base\Model::getFirstError PHP Method

getFirstError() public method

Returns the first error of the specified attribute.
See also: getErrors()
See also: getFirstErrors()
public getFirstError ( string $attribute ) : string
$attribute string attribute name.
return string the error message. Null is returned if no error.
    public function getFirstError($attribute)
    {
        return isset($this->_errors[$attribute]) ? reset($this->_errors[$attribute]) : null;
    }

Usage Example

示例#1
0
文件: BaseHtml.php 项目: Kest007/yii2
 /**
  * Generates a tag that contains the first validation error of the specified model attribute.
  * Note that even if there is no validation error, this method will still return an empty error tag.
  * @param Model $model the model object
  * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format
  * about attribute expression.
  * @param array $options the tag options in terms of name-value pairs. The values will be HTML-encoded
  * using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
  *
  * The following options are specially handled:
  *
  * - tag: this specifies the tag name. If not set, "div" will be used.
  * - encode: boolean, if set to false then the error message won't be encoded.
  *
  * See [[renderTagAttributes()]] for details on how attributes are being rendered.
  *
  * @return string the generated label tag
  */
 public static function error($model, $attribute, $options = [])
 {
     $attribute = static::getAttributeName($attribute);
     $error = $model->getFirstError($attribute);
     $tag = isset($options['tag']) ? $options['tag'] : 'div';
     $encode = !isset($options['encode']) || $options['encode'] !== false;
     unset($options['tag'], $options['encode']);
     return Html::tag($tag, $encode ? Html::encode($error) : $error, $options);
 }
All Usage Examples Of yii\base\Model::getFirstError