Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Zend\Form Error Messages from Controller

this is probably a very simple task, but currently I'm failing horribly at it. I just want to add a custom error to my form when my authentication fails.

What i tried

$form->setMessages(array(
    array('password' => $this->failedLoginMessage)
));    

Unexpected Result

\Zend\Debug\Debug::dump($form->getMessages());
array(0) {}

If i understand the code correctly this should attach an error message to the password element. Actually looking at the setMessages i thought attaching a single-dimension array should have been enough, but it needs the 2nd dimension, too :S I'm just stuck on that simple task, sigh :)

Thanks in advance!

like image 951
Sam Avatar asked Oct 15 '12 13:10

Sam


2 Answers

My comment as answer, as you've requested:

You can also set error messages to an element directly, using:

$form->get('elemName')->setMessages(array('message1', 'message2', ...));
like image 95
Daniel M Avatar answered Sep 18 '22 00:09

Daniel M


Simple solution, read the code correctly... Correct usage as following:

$form->setMessages(array(
    'formElementName' => array(
         // multiple error messages possible...
    )
));
like image 36
Sam Avatar answered Sep 18 '22 00:09

Sam