我试图用Zend创建简单的表单,我需要在大多数情况下使用这个表单,所以我创建了默认表单然后在控制器中我修改它以使用私有函数。但我有两个问题:
表单由文本字段和sumbit按钮组成
这是我的控制器的代码:
这是为了自定义表单
private function getSearchForm($action = '', $name, $type, $placeholder)
{
$urlHelper = $this->_helper->getHelper('url');
$this->_searchForm = new Application_Form_Admin_Search_Search();
$this->_searchForm->setName($name);
$text = $this->_searchForm->getElement('ricerca');
$text->setLabel('Ricerca '.$type);
$text->setName($type);
$text->setAttrib('placeholder', $placeholder);
$this->_searchForm->setAction($urlHelper->url(array(
'controller' => 'admin',
'action' => $action),
'default'
));
return $this->_searchForm;
}
有行动:
public function pneumaticoAction()
{
$this->_searchForm = $this->getSearchForm('pneumaticosearch', 'search', 'pneumatico', 'Ricerca per: modello, marchio o codice');
$this->view->searchForm = $this->_searchForm;
}
public function pneumaticosearchAction()
{
if (!$this->getRequest()->isPost()) {
$this->_helper->redirector('index', 'public');
}
$form=$this->_searchForm;
if (!$form->isValid($this->getRequest()->getPost())) {
$this->render('pneumatico');
}
$values = $form->getValues();
$this->view->assign(array(
"pneumatici" => $this->_modelAdmin->searchPneumatici($values['pneumatico'])
));
$this->render('pneumatico');
}
答案 0 :(得分:0)
第一个问题,无论何时路由到pneumaticosearch
操作,您都不会设置$this->_searchForm
,但是您将其设置为:
$form=$this->_searchForm;
应该是这样的:
$form = $this->getSearchForm('pneumaticosearch', 'search', 'pneumatico', 'Ricerca per: modello, marchio o codice');
第二个问题。运行渲染时,它类似于将$this->view
参数传递给.phtml
。我没有看到你的视图文件,但我想你需要先设置视图:
$this->view->searchForm = $form