Difference between validate() and hasErrors()

Hi everybody,

I have been slowly learning how to use the Yii framework for the past couple weeks. At the beginning of this week, I discovered the simplelogin module, which helped me a lot understand how authentification works. However, there is something that I encountered in the module that I did not quite understand: The difference between validate() and hasErrors(). Can I use hasErrors() without validate()? when should I use one or the other?

validate() will check your model data for errors and then add them to an errors array, hasErrors() will return the array of errors that were found (if any).

You don’t really need to use hasErrors() unless you have some sort of exception case that you want to handle and validate() is called when you do a save().

So if you are just saving a model to a database, you really don’t need to call either of them because save() will do everything you need it to.

Sorry for the late response. After digging up a bit I finally understood whatever was confusing me. Your comment helped too.

Thanks a lot.

What is the difference between these two?