Hello!
I'm currently finishing up the login form for our application (forms never get boring). I made my form pretty much the standard way:
class LoginForm extends Form
{
/**
* Initializing login form
*
* @author Vorta
* @version 1.0
* @since 2.1.0
* @package USER
*/
public function initialize ()
{
$email = new Email('email', [
'class' => 'form-control input-md'
]);
$email
->setLabel('Email:')
->addValidators([
new PresenceOf([
'message' => 'You haven\'t input your email.',
'cancelOnFail' => TRUE
]),
new EmailValidator([
'message' => 'The email you input is not valid.'
])
])
->setUserOption('type', 'input');
$this->add($email);
$password = new Password('password', [
'class' => 'form-control input-md'
]);
$password
->setLabel('Password:')
->addValidators([
new PresenceOf([
'message' => 'Please input your password.'
])
])
->setUserOption('type', 'input')
->setDefault('');
$this->add($password);
}
}
Once a submission is made, if the form is valid, the credentials are sent to our Auth class. Should Auth return an error message, e.g. "Wrong password", I would like to append that message to the password element. Accessing element's appendMessage() method from controller seems to be impossible so I added the following function within the form:
public function appendMessageFor ($attribute, $message)
{
$this->_elements[$attribute]->appendMessage($message);
}
It does append a message to the element, but it is never received by the form. If it happens that form is not valid, then I noticed that this line would add a message that would be displayed on the frontend:
$this->_messages[$attribute]->appendMessage($message);
If the form is valid, the line above wouldn't work as messages is NULL. How can one, if it's even possible, append an error message to a field from controller, even if validation tests succeeded?