views:

1232

answers:

4

Hi,

I have a controller that passes input from a form into a model class to perform validation. If the validation is successful I want to allow the flow to continue and render the default view associated with the controller.

My issue is that if validation is not successful then I want the model to pass back validation error messages and display them in a separate view. How can I set the error messages on the alternative view?

Thanks in advance.

+3  A: 

Why do you want to display the error messages in a different view? Why not build conditionals into the view? Something like if form has errors then echo messages else echo form.

You could use $this->_forward to forward to another action with its respective view. You can pass along whatever you wish. Just pass the form object along, it contains all the error messages. Or you can retrieve certain error messages or all of them from the form object and pass them to a view or an action.

tharkun
Probably should go with same view. In the validate method I am returning an error message based on what validation failed e.g couldnt find item, item outside date range. If it passes I return empty then check in the view if the return value from the validation is not empty? Is this what you mean
db83
It seems you're doing validation check manually. But that's what validators are there fore and then you just need to call $form->isValid() in order to know if all tests have passed. If you need validators which ZF doesn't offer, you can write your own custom validators and plug them in exactly the same way as the built in validators. So you can for example check if ($form->isValid()) and then either send the error messages plus if you wish use $form->populate and send the populated form to the view or send a success message.
tharkun
Additionally you can always make checks in the view as well. Like if ($form->hasErrors) show this, else show that. (!pseudo-code!)
tharkun
Cool. I get it now. I'll look into custom validators now.
db83
A: 

Well, from the controller you can redirect them to another action in another controller:

$this->_forward($newactionname,
                        $newcontrollername,
                        $newmodulename,
                        Array($parameters_to_pass);
    }

or you just just render a different view file:

$this->render('index_alternative');
Richy C.
A: 

FlashMessenger could be part of the solution? http://framework.zend.com/manual/en/zend.controller.actionhelpers.html

Steve
+2  A: 

Don't use _forward() if you are redirecting to actions in the same controller, just call the action directly using $this->fooAction(), rather than this->_forward('foo'...

The reason is performance and errors that can occur due to the controller being constructed wtice. When you call _forward not only the predispatch run again (which is something to be expected) but init() and the constructor also gets called again. If you have your controller extend from other controllers, than all those controllers will be called too, including their init(). If you have code in your init() is it will run twice, and if you are writing to a database it will write the line twice! Avoid the whole thing and call the action directly and use $this->render() instead.

You can easily see this issue if you profile your code,

Dan
+1 dude, I can't believe tharkun said to redirect. :/
sims