We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Can i get Validation message(getMessage) when i use validationHasFailed function?

Can i get Validation message when i use validationHasFailed function if not how to get messages when validation of the model fails ...?

i don't want to save the data in model i just want to know whether validation has failed or nor with failed messages.. i can know if validation had failed using validationHasFailed function. how to get meassges in case validation has failed

What phalcon version you are using? You should call validation method yourself - ti will return boolean true or false if validation was failed. If you want just messages then use $model->getMessages()

edited Feb '17

i am using Phalcon 3.0 can i use $model->getMessages() without calling validate function by just $model->validationHasFailed ?



1.9k
edited Feb '17

just call validation() on model in order to populate messages array. You don't need to save it if you do not want. And after get messages with getMessages()



145.0k
Accepted
answer

In Phalcon 3.0 validationHasFailed shouldn't be used anyway, it's old sutff from Phalcon\Mvc\Model\Validator namespace and shouldn't be used just:

$model->validation(); // to check if valid or not
$model->getMessageS() // to get error messages

So $model->validation() return TRUE or FALSE in case of success and failure of model validation right???

Suppose if there are 3 validations and the model is failed in the first validation itself does it stop there or will it continue validating other 2 validations?

edited Feb '17

It will continue if you don't provide "cancelOnFail" => true, option

https://docs.phalcon.io/en/3.0.1/reference/validation.html#cancelling-validations

i am getting getMessage as an object...how can get the only message instead of getting all the object details

  object(Phalcon\Mvc\Model\Message)[302]
  protected '_type' => string 'Uniqueness' (length=10)
  protected '_message' => string 'Username already allotted' (length=25)
  protected '_field' => string 'user_name' (length=9)
  protected '_model' => null
  protected '_code' => int 0