jblue
jblue

Reputation: 4450

zend_form: should I use one right in the layout

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:

What should I do? Any advice on this?

Upvotes: 2

Views: 455

Answers (1)

Tim Fountain
Tim Fountain

Reputation: 33148

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.

Upvotes: 5

Related Questions