Zend different view scripts? - zend-framework

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.

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');

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,

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.

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

Related

ICommand not binding in Loaded event using MVVMLight framework

I am wondering why binding a button inside the Loaded event in WPF page does not work and will only work after navigating to another page, and going back.
I have an inventory app and on the main page, most of the ViewModel are called because of a Back button which goes back to a specific lists and what causes that is, it will start binding the even if that command is not for that page and it will also load the collections for other pages.
So I used Loaded page event to call the necessary methods to populate the lists and also start binding commands for this specific page. I also used Unloaded page event for clean up like unsubscribing to some CRUD events.
The problem now though is, buttons does not get binding in Loaded page event. I do not know why..
I have made a miniature app to demo the problem. It can be downloaded here
(full source code included)
https://www.dropbox.com/s/qzumzyicuvrktsi/ICommandTest.zip?dl=0
This is because your views are not getting notified about the change of Command_ShowAddWindow and Command_ClickMe. Let me explain:
When your Page constructor is first run the bindings to your commands are initialized and transferred to the view, but by that time your commands are null, so the view binds both buttons' commands to null.
Then when your Loaded event is fired the commands are initialized, but the view is not getting notified about it, so it keeps command bindings to null.
The solutions to the problem are:
You manually call RaisePropertyChanged to notify the view about commands change when you initialize them:
void InitCommands()
{
Command_ShowAddWindow = new RelayCommand(Command_ShowAddWindow_Click);
Command_ClickMe = new RelayCommand(Command_ClickMe_Click);
RaisePropertyChanged("Command_ShowAddWindow");
RaisePropertyChanged("Command_ClickMe");
}
Or you initialize your commands in your ViewModel constructor before DataBindings are initialized:
public ViewModel_Page1()
{
InitCommands();
...
}

Zend framework --- Action code before rendering view

When I use zend framework, how to run action code in controller before rendering view?
Now I use 2 action functions,
The first one have no view. The second one have the expected view.
When the first function is called, at the end of the function it will be redirected to call the second function.
Although I find this method works, the user cannot go back to last page by browser back button.
i don't understand your question so much but ...
Controller is always run before render action. Its a logic which running before render is called. I dont know from your question what you really need =( try be more objective pls. If you need run any code before action in controller on all actions in controller, you use init or preDispatch functions.
http://zfreak.wordpress.com/2011/04/14/usage-of-init-vs-predispatch-methods-in-zend-front-controller-zend-framework/
If you need 2 separated logics, you can make own function or class in your project and call it or create instance anywhere you need. If your problem is with any render as ACL restrictions... i use own function in controller which retrieve error_access_page and after call $this->_helper->viewRenderer->setNoRender(). If my users not have access, this show error_page and no render action's phtml file.
hope help =]

ASP.Net MVC 3 - Single controller, single action and multiple views

I would like to know what is the best method to have an action return different views. Let's say you have a form for submitting data, but you want to choose the view depending on what data is submitted. I would prefer not using a redirection, since there is stuff I want to be displayed in the data that is posted.
An example of this would be to have an Edit form that displays a Details view when clicking on Save, but without using a redirection.
I know this could be done with a single view containing a conditional if statement to display this or that, but there are cases where I would prefer my views to stay simple without too much code in them. If the controller could just choose the view to display once the data is posted, this would be great.
There is an overload to the View() method that allows you to specify the name of the View you want to return.
return View("DetailsView", model);
You should be using the Post/Redirect/Get pattern. You can still "display stuff." You can pass an ID on the URI and look them up in the new GET or use TempData.
Attempting to circumvent Post/Redirect/Get is not a good solution. Among other things, it breaks the back button.

Zend Framework: How to call a controller action helper from a view script?

I have a custom controller action helper that I would like to be able to call from a view script. How can I achieve this?
Solution:
Warning: You probably don't really want to call an action helper from your view script unless you aren't using any dispatch hooks. But if you really, really want to call your action helper:
$helper = Zend_Controller_Action_HelperBroker::getStaticHelper('Myhelper');
Afaik, you can't, and you should not that's MVC. Trying to solve such problem should be a potential warning on your design.
However, in some case you may need to achieve a similar thing.
For example, the flashMessenger() action helper aims to provide a simple way to share messages between requests, however it is not available in the view, you need to manually pass it to the view. I've myself written a wrapper to be able to use as a view helper.
So maybe try to be more explicit on what you're trying to achieve, and we may help you to know if there isn't a good alternative.

Load model data in background

I am developing a Prism application where I need to load some data in the model in the background after the application has loaded. I have a separate view which is hidden which would show this data via a view model. I dont want to increase the application load time and neither do I want to increase the view load time. The only solution I could think of is to raise a Shell Loaded event and let the view model or model subscribe to it.
Is this a good approach or is there a better approach?
Also, I am not sure whether I should raise the Shell Loaded event in the Run of the Bootstrapper or in the Shell ViewModel. How do I link a routed command (for ex: Loaded) to an ICommand
I try to avoid to handle these kinds of events in the bootstrapper. So attaching the Loaded event of the shell view to the shell view model is a good way to go. The only reason to actually raise this event in the bootstrapper is if you need the StartupEventArgs (which I sometimes do).
There are quite a few ways of handling the loaded event.
100% MVVM use an attached property that passes the event straight to an ICommand or method. Ex: how to call a window's Loaded event in WPF MVVM?
Just handle the Loaded event it in the code behind and call the Loaded method / Command in the VM from there. This a simple and easy solution to get started.
To reduce loading time, you should try to avoid putting too much work on the UI thread. You may want to investigate the TPL or a nice framework called ReactiveUI which provides a very convient mechanism to load things asynchronously.