Zend different view scripts?
Asked Answered
R

4

8

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.

Rubalcava answered 13/8, 2009 at 13:34 Comment(0)
W
11

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');
Willmert answered 13/8, 2009 at 13:40 Comment(0)
A
9

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,

Aminaamine answered 6/3, 2010 at 8:29 Comment(0)
I
4

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.

Interoffice answered 13/8, 2009 at 13:39 Comment(4)
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 meanRubalcava
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.Interoffice
Additionally you can always make checks in the view as well. Like if ($form->hasErrors) show this, else show that. (!pseudo-code!)Interoffice
Cool. I get it now. I'll look into custom validators now.Rubalcava
A
0

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

Apnea answered 6/10, 2009 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.