yii2 rewrite model addError

315 Views Asked by At

this is original Model.php function

public function addError($attribute, $error = '')
{
    $this->_errors[$attribute][] = $error;
}

in my model property i want override this method in this way

public function addError($attribute, $error = '')
{
    if ($attribute == 'status'){
        $this->_errors[$attribute] = $error;
    }
    else{
        $this->_errors[$attribute][] = $error;
    }
}

I can't access at $this->_errors ... I have no idea how i can override this funcion without modify main model.php

1

There are 1 best solutions below

0
On

found solution! In personal model class add this

/**
 * @param array $errors
 */
public function setErrors($errors)
{
    $this->errors = $errors;
}

and in my validate function

$this->setErrors(['status' => false, $attribute => 'not found']);

without use addError function