yii\base\Model::getErrors PHP Method

getErrors() public method

Returns the errors for all attributes or a single attribute.
See also: getFirstErrors()
See also: getFirstError()
public getErrors ( string $attribute = null ) : array
$attribute string attribute name. Use null to retrieve errors for all attributes.
return array errors for all attributes or the specified attribute. Empty array is returned if no error. Note that when returning errors for all attributes, the result is a two-dimensional array, like the following: ```php [ 'username' => [ 'Username is required.', 'Username must contain only word characters.', ], 'email' => [ 'Email address is invalid.', ] ] ```
    public function getErrors($attribute = null)
    {
        if ($attribute === null) {
            return $this->_errors === null ? [] : $this->_errors;
        } else {
            return isset($this->_errors[$attribute]) ? $this->_errors[$attribute] : [];
        }
    }

Usage Example

 /**
  * @param Model $model
  * @param string $message
  * @param int $code
  * @param Exception $previous
  */
 public function __construct(Model $model, $message = null, $code = 0, Exception $previous = null)
 {
     $this->model = $model;
     if (is_null($message) && $model->hasErrors()) {
         $message = implode(' ', array_map(function ($errors) {
             return implode(' ', $errors);
         }, $model->getErrors()));
     }
     parent::__construct($message, $code, $previous);
 }
All Usage Examples Of yii\base\Model::getErrors