views:

110

answers:

1

I usually instantiate my forms in an action and that's where I process them when they're submitted. I then pass them on to the view and output them there as usual.

This form (a search box) is different because it's not part of a single page. It has to be visible everywhere. I've made it part of the template layout.phtml and instantiated and accessed it right there.

$search = new SearchForm(); 
echo $search;

The form prints out fine but the question now is where do I handle this form. I usually have processing code like this in the action..

    if ($this->_request->isPost()) {
        //and form is valid
        //process the data            
    }

but since this form is universal, there's no action for it. How should I handle this?

Should I:

  • create a dummy action for it (which doesn't make sense because the form is everywhere)
  • or should I put the processing code right into the layout.phtml (which I think is bad MVC practice because I'm now mixing processing the form with the view).

What should I do? Any advice on this?

+5  A: 

Surely the search will need some processing code to build up the results, so I would create this action somewhere generic (like on your IndexController) and point the form at that. Even if the form is on every page it's perfectly fine for you to point it at a specific URL like /search/.

Otherwise you could create a controller plugin that checks the request to see if it has been submitted, and then runs the processing code.

Tim Fountain
I would rather create a dedicated controller than a generic function. It doesn't hurt and you can still redirect to other controllers from there if that is necessary. +1 for the controller plugin.
Gordon
Yep, a url/controller to process the submission. Ex: if it's a login form that appears in the site layout, perhaps handle it at `AuthController::loginAction()`.
David Weinraub