Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Phalcon\Validation get failed fields

When validation is done, there is a list of messages returned. Is there a way to get the fields whose validations are failed? Not just a list of messages.

Example:

        $this->add('name', new PresenceOf(array(
            'message' => 'The name is required'
        )));

        $this->add('email', new PresenceOf(array(
            'message' => 'The e-mail is required'
        )));

        $this->add('email', new Email(array(
            'message' => 'The e-mail is not valid'
        )));

If third validation is failed, how can I know that email is not good, but name is fine?



5.9k
Accepted
answer

please take a look phalcon docs http://docs.phalconphp.com/en/latest/reference/validation.html

you can get failed field name using this code

$messages = $validation->validate();
if (count($messages)) {
    foreach ($validation->getMessages() as $message) {
        echo "Message: ", $message->getMessage(), "\n";
        echo "Field: ", $message->getField(), "\n";
        echo "Type: ", $message->getType(), "\n";
    }
}


6.8k

I'm sorry... it did read that doc but for some reason didn't see that info. Just need to sleep more :)