2010-10-11 63 views
0

这里是我的代码:Zend_Form中不显示数据

<?php 

class IndexController extends Zend_Controller_Action 
{ 

    public function indexAction() 
    { 
     $this->_forward('search'); 
    } 

    public function searchAction() 
    { 
     if($this->getRequest()->isGet()) 
     { 
      $form = $this->getForm(); 

      $value = $form->getValue('search'); 
      echo "'", $value, "'"; // <-- prints '' 
     } 

     $this->view->form = $this->getForm(); 
    } 

    public function getForm() 
    { 
     $form = new Zend_Form(); 
     $form->setAction('/index/search') 
      ->setMethod('get'); 

     $search = $form->createElement('text', 'search'); 
     $search->addFilter('StripTags') 
       ->addFilter('StringTrim'); 

     // Add elements to form: 

     $form->addElement($search) 
      ->addElement('submit', 'submit', array('label' => 'Search')); 

     return $form; 
    } 
} 

searchAction() $值始终是空白,甚至在我提交表单,我看到在URL中的数据。问题是什么?

编辑:我修正了getValue()getValues()但它仍然不工作。

回答

2

在你能够使用 - > getValue();你必须验证表单。

public function searchAction() 
    { 
     if($this->getRequest()->isGet()) 
     { 
      $form = $this->getForm(); 

      if ($form->isValid()) { 
       $value = $form->getValue('search'); 
       echo "'", $value, "'"; // <-- prints '' 
      } else { 
       // what ever 
      } 



     } 

     $this->view->form = $this->getForm(); 
    } 
0

你想要的功能是getValue,而不是getValues。

一个微妙的区别,但他们做了两件完全不同的事情。

+0

好的,我固定,但仍然没有爱。我无法获得任何价值,只有空虚。 – moteutsch 2010-10-11 16:34:08

1

你需要传递$this->_request->getParams()$form->isValid(),否则表单不会有任何价值的工作。