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

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?



7.9k
Accepted
answer

please take a look phalcon docs https://docs.phalcon.io/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";
    }
}


10.7k

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