Zend-form in controller and processing this form in specific action - how can I do this?
Im my layout I have search form, so it's displayed on every page. It looks like this:
<?
$form = new SearchIndexForm();
$form->setAction($this->configuration['BaseUrl']Index/search);
echo $form; ?>
I would like to have sth like that: when someone click submit button, the form redirects to the action "search" on "Index" controller, where there is processing the form values.
So, what should I do, to have开发者_高级运维 equivallent to this, which will be working with data send by post method from form in layout?
if($this->_request->isPost()){
$formValues = $this->_request->getParams();
if ($form->isValid($formValues)){
...
}
}
With the above, when I click submit, it gets me to the /Index/search, but nothing happens...
The form itself worked perfectly when it was in one action.
It seems like you're doing everything right but I think that there is some code that you should change.
Try the following code
$request = Zend_Controller_Front::getInstance()->getRequest();
//or you can try
//$request = $this->getRequest();
if($request->isPost()){
$form = new SearchIndexForm();
if ($form->isValid( $request->getPost() ) ){
echo 'This should output if the form is valid' . PHP_EOL;
}
}
I don't like accessing variables directly as you did in $this->_request because Zend might have to do doing things to the variables to make them 'proper'. I know that sometimes nothing is done but better safe than sorry. Unless you're positive about it, which I'm usually not positive about unless I've really looked at the code.
加载中,请稍侯......
精彩评论