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

Validation change protected field to public

I am trying to fetch the field name, that im currently validating. But the virable is a protected function, how can i print it out? I tried to change public $_field inside my custom validation class, that extends the validation class. But it dosent work...

How can I print it out?

$validation = new \Validation\Validate();

    $messages = $validation->validate($_POST);

    if (count($messages)) {
        foreach ($messages as $message) {
            echo $message->_field;
        }
    }

I found out that i could use this function:

$message->getField()

edited Jul '15

A little idea u can use- new function in model: (example is from here https://docs.phalcon.io/en/latest/reference/validation.html for more details)

    public function getMessages($filter = NULL)
    {
        $messages = [];

        foreach (parent::getMessages($filter) as $message) {
            switch ($message->getType()) {
                case 'PresenceOf':
                    $messages[] = $message->getField().": Field is required");
                    break;
                default:
                    $messages[] = $message->getMessage();
            }
        }

        return $messages;
    }