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.
!isValid()
should generate the form from there setting up the additional decorators?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.]