¿Cómo puedo agregar un mensaje de error a un formulario?

I am validating a user login and would like to attach an error message to the form if they the user submits details that do not authenticate.

In FieldSet I can see function setMessages() but this only appears to match and set against an element key.

How can I attach an error message to the form and not to a form element?

The following code is in within the LoginForm class.

public function isValid()
{
    $isValid = parent::isValid();
    if ($isValid)
    {
        if ($this->getMapper())
        {
            $formData = $this->getData();
            $isValid = $this->getMapper()->ValidateUandP($formData['userName'], $formData['password']);
        }
        else
        {
          // The following is invalid code but demonstrates my intentions
          $this->addErrorMessage("Incorrect username and password combination");
        }
    }

    return $isValid;
}

preguntado el 28 de agosto de 12 a las 10:08

2 Respuestas

The first example is validating from a database and simply sending back an error message to the form:

//Add this on the action where the form is processed
if (!$result->isValid()) {
            $this->renderLoginForm($form, 'Invalid Credentials');
            return;
        }

This next one is adding simple validation to the form itself:

//If no password is entered then the form will display a warning (there is probably a way of changing what the warning says too, should be easy to find on google :)
$this->addElement('password', 'password', array(
            'label'    => 'Password: ',
            'required' => true,
        ));

Espero que esto sea de utilidad.

Respondido 28 ago 12, 10:08

Thanks David, already have the solution working fine, the only think that is missing is the error message if the authentication service API returns an invalid login. - Thomas-Peter

Sorry, I don't understand... wouldn't the top one be the correct solution? Post some code, hopefully that'll shed some light. - david sigley

In ZF1: in order to attach an error message to a form - you can create a decorator element for this:

Tomado de:

http://mwop.net/blog/165-Login-and-Authentication-with-Zend-Framework.html

class LoginForm extends Zend_Form
{
    public function init()
    {
        // Other Elements ...

        // We want to display a 'failed authentication' message if necessary;
        // we'll do that with the form 'description', so we need to add that
        // decorator.
        $this->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')),
            array('Description', array('placement' => 'prepend')),
            'Form'
        ));
    }
}

And then as an example in your controller:

// Get our authentication adapter and check credentials
$adapter = $this->getAuthAdapter($form->getValues());
$auth    = Zend_Auth::getInstance();
$result  = $auth->authenticate($adapter);
if (!$result->isValid()) {
    // Invalid credentials
    $form->setDescription('Invalid credentials provided');
    $this->view->form = $form;
    return $this->render('index'); // re-render the login form
}

Unsure if this still works in ZF2

Respondido 28 ago 12, 15:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.