Html::error($Model,$Attribute) Not Working

I am trying to add an error tag in the form.


<?= Html::error($model, 'image');?>

But I get an ErrorException

[b]

htmlspecialchars() expects parameter 1 to be string, array given.[/b]

On the other hand Active Label works fine on the same field


<?= Html::activeLabel($model, 'image');?>

Tracking back, on the BaseHtml.php


$error = $model->getFirstError($attribute);

The $error is an array, and the encode() function expects it to be an string


1. in C:\wamp\www\yii-branty\vendor\yiisoft\yii2\helpers\BaseHtml.php at line 95

     * @see decode()

     * @see http://www.php.net/manual/en/function.htmlspecialchars.php

     */

    public static function encode($content, $doubleEncode = true)

    {

        return htmlspecialchars($content, ENT_QUOTES, Yii::$app->charset, $doubleEncode);

    }

 

    /**

     * Decodes special HTML entities back to the corresponding characters.

     * This is the opposite of [[encode()]].

2. in C:\wamp\www\yii-branty\vendor\yiisoft\yii2\helpers\BaseHtml.php –	 yii\helpers\BaseHtml::encode() at line 1038

   {

        $attribute = static::getAttributeName($attribute);

        $error = $model->getFirstError($attribute);

        $tag = isset($options['tag']) ? $options['tag'] : 'div';

        unset($options['tag']);

        return Html::tag($tag, Html::encode($error), $options);

    }

That’s very strange, because $model->getFirstError is supposed to return a string:




	/**

	 * Returns the first error of the specified attribute.

	 * @param string $attribute attribute name.

	 * @return string the error message. Null is returned if no error.

	 * @see getErrors()

	 * @see getFirstErrors()

	 */

	public function getFirstError($attribute)

	{

		return isset($this->_errors[$attribute]) ? reset($this->_errors[$attribute]) : null;

	}



I got where I was wrong.

In the controller I had added


$model->addError('image', $model->getErrors('categories_image'));

Where the second parameter is an array.

So when I changed it to


$model->addError('image', $model->getFirstError('categories_image'));

It worked fine.

Thnx Orey…