What is the hasErrors () method used for? It is not clear to us, can someone help, tell me how to interpret its description correctly?

Returns

Returns the value of the indicator / pointer when it finds any error.

We do not know how to understand this.

http://www.yiiframework.com/doc-2.0/yii-base-model.html#hasErrors()-detail

public function validatePassword($attribute, $params) { if (!$this->hasErrors()) { $user = $this->getUser(); if (!$user || !$user->validatePassword($this->password)) { $this->addError($attribute, 'Incorrect username or password.'); } } } 
  • one
    Returns a value indicating whether there is any validation error ......... that is, true/false ....... there are errors - returns true , there were no errors during the check - returns false ... .... and does not return pointers / indicators .... where did you find such a translation .... - Alexey Shimansky
  • @ Alexey Shimansky Thank you very much for the answer. Doesn’t this validate() method do the same? I understand that you could just validate() call instead of hasErrors() . If it doesn't bother you, could you give your answer, in the form of an answer, for future users? - FiR
  • one
    validate course a wrapper over hasErrors and in the result of its work return !$this->hasErrors() , but at the same time it itself is deeper, because There are many other various checks inside (you can even look at Model.php -> validate() ), and also apply beforeValidate and afterValidate where there can still be a bunch of any checks ... and in fact apply to a specific script ... .... hasErrors not tied to scripts, just a dull little simple method - Alexey Shimansky
  • @ Alexey Shimansky Is this 100% accurate information? Thank. Have a nice day. - FiR

1 answer 1

The correct description has already been given in the comments, I will give a small practical example.

Suppose you have user authentication by login and password, and the user can also be in the bath.

It is logical to highlight the inscription "this user is banned" only after successfully entering the login and password. That's what you actually do: you checkBan() method after checking the login and password and inside the if (!$this->hasErrors()) {