why aren't error messages showing on zend_form if validation fails? - zend-framework

I'm trying to get the standard error messages to show up in zend_form but they don't.
I have this:
if ($form->isValid($formData)) {
// do stuff
} else {
$form->populate($formData);
$this->view->form = $form;
}
When I post an invalid form, the form does show up in the view like it's supposed to, but from the tutorials it seems like error messages should show up by default?
What am I missing?
Thank you for your help!

The error messages are applied using Decorator Pattern. There are some Zend Form Element Decorators present in the form by default.
I guess you have overwritten the default decorators, using e.g. setDecorators().

Related

Symfony 2.x Form Field Name

When I render a form, form Filed Name is given as an array. For example: search[item], search[keyword] etc. where search is name of the form.
I'm not great on working with forms but I think, the name should be rendered as simply, name="item" or name="keyword".
I've looked at all the documentation, customizing form rendering topic etc. but I can't find any way to change the default behaviour of Symfony form to render form filed name from 'search[item]' to 'item'.
This way, when I ask for the POST data, I can ask simply $this->getRequest()->request->get('item'), as I have to deal with lots of individual parameters.
Help would be great i) To figure out how to achieve what I want. ii) to let me know, why the name is rendered this way. is this the good practice?
Rather than accessing parameters from the Request object, you can bind the Request object to the form.
For example, in your controller method that you post your form to:
namespace Acme\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\Form\MyFormClass;
class MyFormController extends Controller
{
receiveFormAction(Request $request)
{
$form = new MyFormClass();
// you can specify that a route only accepts a post
// request in the routing definition
if ($request->isMethod('POST')) {
// this populates the form object with the data
// from the form submission
$form->bind($request);
if ( ! $form->isValid()) {
throw new \Exception('Invalid form');
}
// an array of the data the format you require
$data = $form->getData();
$data['item'];
$data['keyword'];
// etc.
}
}
}
The above is the way you should be handling forms in Symfony 2, and is how you can leverage the power that the forms component gives you, with validation etc.
Symfony supports multiple forms on a page. They might be instances of the same form or have similar field names. Having the fields for each form all together in an array makes this easy to do.

No validations errors to display in symfony 1.4 form

I have a problem with all my forms in symfony 1.4. They work with good datas, my new objects are created ... But when i give bad datas, my forms do nothing as expected but have no errors.
My code in my action :
$this->form = new EtablissementForm();
$this->form->bind(
$request->getParameter($this->form->getName()),
$request->getFiles($this->form->getName())
);
Edit :
Solved. I guess i need some sleep. This was really a dumb mistake. I was sure i had removed a new E...Form(); In my view for my action, actually i did it ... in another file.
So my $form was overwritten by an empty form. So the problem is solved.
if ($this->form->isValid())
{
//some things and a redirect
}
In a nutshell, my form works with good datas. But i don't have any errors to display when i'm giving bad datas. And my form don't add something in my database. Validations works cause it raise exception in bind, but i just get an empty form to display.
Your view needs to have proper scope to $form. Your code above may be incomplete. In the action you show above, if the action ends, and you go to your view, and your view has the following, your validation errors should appear inline with your form.
<?php echo $form ?>

Must I re-populate Zend_Form fields after the post back manually?

After a post back, suppose validation fails and I want to show the form again with errors, I find that the form is empty, must I repopulate the form fields manually?
The method isValid populate the form field. You don't have to repopulate manually.
I find the best way to handle form processing is to use something like
$form = new My_Form;
if ($this->getRequest()->isPost()
&& $form->isValid($this->getRequest()->getPost()) {
// process form and redirect (PRG pattern)
}
$this->view->form = $form;
This way, your form is shown on the first request and if not valid, is re-shown with the submitted values and any validation messages.
In case you are using some custom isValid don't forget your form also has a populate() function.
So
$data = $this->getRequest()->getPost();
if(!$myForm->isValid($data)){
$myForm->populate($data);
}
No need to do it by hand thats for sure.
Of course dont forget to assign the same object you did the isValid - populate calls on
$this->view->form = $myForm;
After the checks.

Zend Framework: Post to different action then return to original action if fails validation AND keep form fields

This might sound like an odd scenario, but I've got two forms on one page. One is just posting back to itself. I made the second post to another action to keep the code cleaner. Maybe not the right choice...
The problem I'm having now is that if that second form doesn't validate, I redirect back to the page with the form but I don't know how to keep my form fields filled in with the original information the user entered. Is there a way to do that and keep posting to two separate actions, or do I need to just bite the bullet and have both forms post back to the same action and deal with the messy logic?
I would submit both forms to the same action. There really shouldn't be anything too messy about it. In each form include a hidden field to signify which form is being submitted.
Application_Form_Login:
/* other form elements */
$this->addElement('hidden', 'login', array(
'value' => 1
));
Application_Form_Register:
/* other form elements */
$this->addElement('hidden', 'register', array(
'value' => 1
));
Controller:
$loginForm = new Application_Form_Login();
$registerForm = new Application_Form_Register();
if($this->_request->isPost()) {
if($this->_request->getPost('login')) {
if($loginForm->isValid($this->_request->getPost())) {
// validated, redirect
$this->_helper->redirector('profile', 'user');
}
}
if($this->_request->getPost('register')) {
if($registerForm->isValid($this->_request->getPost())) {
// validated, proceed as needed
}
}
}
$this->view->loginForm = $loginForm;
$this->view->registerForm = $registerForm;
View:
echo $this->loginForm;
echo $this->registerForm;
With this type of a setup, if either of your forms fail validation, isValid() will preserve any data that has been entered and you still redirect on a successful validation of one or both of the forms.
Personally, I think that each form should post to its own controller, as you have. This keeps the code for processing that form in a single place. The issue here is that you want to return to the original page on failed validation. But why? Why not simply redisplay the form in the target controller, just like you would if there were a single form on the page?
For example, consider a login form that appears on every page of a sie (perhaps because it in the site template/layout). It posts to something like AuthController::loginAction(). If the login fails, then you don't typically send him back to the page from which he came. You leave him at the login page, with the form as pre-filled from the $_POST as you want it to be (probably a username, but not his password).
See this answer for a similar discussion.
Update: Had another thought in this. If you really want to handle the processing in two different controllers in order to keep him on the page from which he posted the form, at least extract that form processing out into an action helper. This way, you could at least keep that form-processing DRY.
Update: Rob Allen has just written a great blog post "A form in your layout" in which he describes a method that uses an action-helper with a preDispatch() method that instantiates and processes the form. Very nice.
How do you redirect? I don't see the problem if you just display the form page again. You can prefill you forms using Zend_Form::populate().
Well, I would just keep both forms submitting on the same page.
I don't see why your code should get any less readable. Learn how to use action helpers and your controllers will suddenly look extremely simple and readable:
public function indexAction()
{
$request = $this->getRequest();
// send forms to view, so we can print them
// but also so we can access them in action helpers
$this->view->form = $this->_getForm('Form1', '/');
$this->view->form2 = $this->_getForm('Form2', '/');
if ($request->isPost())
{
// process the first form
if (isset($_POST['form_submit_button']) && $this->view->form->isValid($_POST))
{
$this->_helper->form($this->view->form->getValues());
}
// process the second form
else if (isset($_POST['form2_submit_button']) && $this->view->form2->isValid($_POST))
{
$this->_helper->form2($this->view->form2->getValues());
}
}
Each form's processing would have its own action helper.

triggering invalid message with zend_dojo elements on post fail

I am having a few issues with a Zend_Form that I have which uses Dojo elements to handle user validation.
The scenario is when are user fills in the form the dojo elements ensure the formatting is correct. On post of the form if there is an error such as the email address already existing in the database, my code throw an exception that I catch, I want to then use the dojo validation to display the error rather than having error messages at the top or bottom of the form.
I've tried the following:
catch(Exception $e){
$signupForm->populate($formData);
$signupForm->getElement('email')->setInvalidMessage('email addresss already exists');
$this->view-form = $signupForm;
}
This redisplays the form but does not highlight the dojo element to show what element is failing. How can this be done or am I going to have to display the error messages somewhere on the form in an list?
Any help would be gratefully received.
Did you take a look at Zend_Dojo_Form?