سؤال

I am really novice to ZF, have Googled a little bit to find a way to decorate login forms after the user have submitted the form (eg. Login form). After the submission and processing if it is not valid I want to show twitter bootstraps alerts above the field/s that are filled incorrect.

  1. Should I create a model that controls the form and if the form !isValid() should generate the form from there setting up the additional decorators?
  2. Should I set everything in the session and clear them variables after I render the view and decorate ?
  3. Or should I use some other Mystical Method?
هل كانت مفيدة؟

المحلول

I tend to keep the errors inline with their elements. But, like you, I also prefer to have to a form-level alert at the top that indicates to the user that there is something to fix.

For that, I typically add a flag in my controller:

if ($form->isValid($postData)){
   // write to db and redirect
} else {
    $this->view->hasFormError = true;
}
$this->view->form = $form;

Then in the view-script:

<?php if ($this->hasFormError): ?>
<!-- my alert box, using Bootstrap or whatever -->
<?php endif; ?>
<?= $this->form ?>

It is almost certainly slicker to add a FormError decorator to the form itself. It checks to see if any of the form elements had an error and then renders the alert. Then your controller and your view-script become much leaner:

Controller:

if ($form->isValid($postData)){
   // write to db and redirect
}
$this->view->form = $form;

View-script:

<?= $this->form ?>

But when faced with defining another decorator, setting decorator prefixes, and then adding it to the form, I confess that I usually cheap-out and use the in-controller flag.

[In principle, I think the whole idea of applying decorators should be part of the view-layer, not part of the form definition itself. Under that philosophy, I would keep the form undecorated until we hit the view-script, at which point we could apply some view-helper that adds all the decorators. This is - in a somewhat modified sense - what ZF2 does. But I have never had the time to pursue that path too deeply.]

نصائح أخرى

These are the thow methods I use to display error messages:

I left the standard Error decorator un change on all my fields to display the specific message (ie: invalid Email)

I also use the flashMessenger to display a general message like

Errors occured while validating the form:
* Email
* ...

The flashMessenger is an action helper that has an equivalent view helper. The flashMessenger is a one hop session message which is cleared from session automatically once displayed

More on the flashMessenger.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top