我在我的zf2应用程序中使用zend表单,它有两个文本框。一旦我提交了数据,我就把它拿出来并过滤我用来在桌面上显示它的数据数组。一旦数据被过滤并显示在表格上,表格数据就会重置。
即使提交后,有没有办法在表单中保留值?
这就是我拥有的代码,点击一下按钮即可进入此页面,操作如下所示。所以这个页面有一个表格,它将根据我拥有的两个文本框进行填充。当我单击提交按钮时,文本框中的数据将消失,数据将填充到表中。
所以我想在这里做的是,保留输入的值在文本框中。我能这样做吗?我知道HTTP是一种无状态协议。我知道我可以使用前端客户端技术来做到这一点,只是想知道是否有解决方法。
public function searchAction()
{
//search form instantiation
$form = new SampleForm();
// get the post request
$request = $this->getRequest();
//instantiation of select for querying
$select = new Select();
// get parameters
$artist = $request->getPost('artist');
$title = $request->getPost('title');
//prepare search criteria array
$search = array('artist' => $artist, 'title' => $title);
//remove empty criteria from the search criteria list
foreach ($search as $key => $value) {
if (empty($value)) {
unset($search[$key]);
}
}
$result = $this->getAlbumTable()->fetchAll($select->where($search));
return array(
'albums' => $result,
'form' => $form,
);
}
答案 0 :(得分:0)
我终于明白了我在做什么。我只是必须这样做。
if ($request->isPost()) {
$album = new Album();
$form->setInputFilter($album->getInputFilter());
$form->setData($request->getPost());
}