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

How to loop through form validation messages in volt?

I have in my volt template...

{{ this.flashSession.output() }}
{{ form.messages('username') }}
{{ form.messages('password') }}
{{ form.messages('confirmPassword') }}

This is in the form file...

public function messages($name)
    {
        if ($this->hasMessagesFor($name)) {
            foreach ($this->getMessagesFor($name) as $message) {
                $this->flash->error($message);
            }
        }
    }

It seems I have to put a form.messages for every field in the form or they wont print/render. My flash is using DI direct and I have a DI flashSession setup for use before redirects too.

Is there an easier way in volt to just loop through ALL available form.messages IF they exist and print/render them?

edited Nov '18

I think i solved this ... i made 2 functions. One for printing validation errors from a specific field. And one for printing all of them. Use one or the other.

In the form... class SomeForm extends Form

public function message($name)
    {
        if ($this->hasMessagesFor($name)) {
            foreach ($this->getMessagesFor($name) as $message) {
                $this->flash->error($message);
            }
        }
    }
    public function messages()
    {
        foreach ($this->getMessages() as $field => $message) {
            $this->flash->error($message);
        }
    }

in the volt...

{{ form.messages() }}