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

Appending message to form

Hello,

I'm trying to add error message into form by calling $form->getMessages()->appendMessage(new \Phalcon\Validation\Message("Custom error"));, but when in view i'm trying to recieve messages count($form->getMessages()) i get empty list.

Is it a bug or there is some other way to add messages into form?

My guess is that $form->getMessages() is returning a value, not a reference, so appending to a static value isn't going to save back into the form? I wouldn't know without digging through the source code. But that is my guess.

Try setting the message directly to the form. This is how it is handled elsewhere in phalcon, i can't imagine they would implement it differently here.

$message = new Message($text, $field, $type);
$form->appendMessage($message);

full disclosures i haven't tested this...

I was wrong. Looking at source now.



85.5k

if i do this in the controller

$messages = $form->getMessages('name');

        $messages->appendMessage(new  \Phalcon\Validation\Message("yohooo") );

        echo "<pre>";
        var_dump($messages);
        echo "</pre>";
        exit;

there are some messages inside. however if i print them in view they are gone


$form = $this->view->getVar("form");

echo "<pre>";
var_dump($form->getMessages()); //prints null in view
echo "</pre>";
exit;

i guess you can report this in github as an issue

It completely depends on what getMessages() is passing back. if its not a reference then of course appending to it wont do anything.

I wish i could simply attach messages to forms themselves, instead of having to attach them to form elements.

if i do this in the controller

$messages = $form->getMessages('name');

       $messages->appendMessage(new  \Phalcon\Validation\Message("yohooo") );

       echo "<pre>";
       var_dump($messages);
       echo "</pre>";
       exit;

there are some messages inside. however if i print them in view they are gone


$form = $this->view->getVar("form");

echo "<pre>";
var_dump($form->getMessages()); //prints null in view
echo "</pre>";
exit;

i guess you can report this in github as an issue

yeah the source code just passes back and object, its it out of scope of the parent object,

let group = new Group();
if typeof messages == "array" {
  for elementMessages in messages {
     group->appendMessages(elementMessages);
  }
}
return group;


2.7k

So, what is the best/right way to add a message into the form?



85.5k

this is the correct way, there is a bug i opened a issue in git for it. I will check if i can fix it tonight/tomorrow https://github.com/phalcon/cphalcon/issues/12037