after some questions about how to resolve one of my problem (http://stackoverflow.com/questions/13609611/using-several-modules-in-the-same-view) I have another one.
I made a form in a view helper because I needed to access the result of this form in another controller.
So here is my view helper called QuickSearch.php
class Zend_View_Helper_QuickSearch extends Zend_View_Helper_Abstract
{
public function quickSearch()
{
$form = new Application_Form_QuickSearchForm();
return $form;
}
}
calling QuickSearchForm.php
class Application_Form_QuickSearchForm extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$this->setAction('/search/quicksearch');
$this->addElement('text', 'searchLocation', array(
'label' => 'Location:',
'required' => true,
'filters' => array('StringToUpper')
));
//[some other elements]
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => 'Search',
));
}
In my main page view, I call the viewhelper with
echo $this->quickSearch();
Which works, since I have access to my form.
When I submit my form, the /search/quicksearch is called as it’s supposed to be but when I try to access the data from the form, it seems that it’s empty.
Here is my Search controller (SearchController.php)
class SearchController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// action body
}
public function quicksearchAction()
{
$form = new Application_Form_QuickSearchForm();
if ($this->getRequest()->isPost())
{
echo("post");
$data = $form->getValues();
echo($data['searchLocation']);
}
}
}
I put the echo(“post”) to see if I was getting a POST request, and every time it works.
the only thing that display is the ‘post’ and the second echo doesn’t display anything.
I don’t know what I didn’t understand in the way to get the form’s data from another controller.
Can someone help me with this ? I just don’t know why it doesn’t work.
You need to pass the post data to the form object. The typical way to do this is by calling the
isValid()method of the form (which also validates the submitted data):