Home > Zend Framework > [ZF] Zend_Form : Remove the submit-button value from url parameters

[ZF] Zend_Form : Remove the submit-button value from url parameters

Zend_Formでフォームエレメントを作る場合にはnameが必須なんだけど、
GET送信するとnameがあるばかりにボタンまでURLに含まれてしまうので
Redirectorによる回避策を試してみた。

検索のフォーム。methodをpostにする。

PHP:
  1. class Application_Form_Search extends Zend_Form
  2. {
  3.     functions init(){
  4.     $this->setMethod('post')
  5.         ->clearDecorators()
  6.         ->addDecorator('FormElements')
  7.         ->addDecorator('HtmlTag',array('tag'=>'div', 'class' => 'zend_form'))
  8.         ->addDecorator('Form');
  9.            
  10.     $this->addElement('text', 'keyword', array(
  11.         'label'      => 'Keyword',
  12.         'required'   => true
  13.     ));
  14.    
  15.     $this->addElement('submit',
  16.         'submit',
  17.         array(
  18.             'label'    => 'Search',
  19.             'required' => false,
  20.             'ignore'   => true
  21.         )
  22.     );
  23.     }
  24. }

検索する場所は仮にindexとして、index.phtmlには検索フォームを表示しておく。

getRequest()->isPost()で送信された値があり、バリーデーションもtrueなら
送信された値($params)からボタンを削除(unset)する。
アクションヘルパーのRedirectorに$paramsを渡してsearchActionにリダイレクトする。

PHP:
  1. public function indexAction()
  2. {
  3.     $this->view->searchForm = $this->_searchForm;
  4.    
  5.     $request = $this->getRequest();
  6.     $params = $request->getPost();
  7.    
  8.     if($request->isPost() && $this->_searchForm->isValid($params)){
  9.         unset($params['submit']);
  10.         $this->_helper->Redirector->setGotoSimple('search', null, null, $params);
  11.     }else{
  12.         $this->view->data = $this->_mapper->fetchAll();
  13.     }
  14. }

※$this->_searchForm ---> instance of Application_Form_Search
※$this->_mapper ---> instance of DataMapper

searchActionに飛ばされた時、検索キーはZendライクなGETパラメーター(/key/value/のパターン)になっているので、
getRequest()->getParams()でキーワードとかを取り出して使う。

PHP:
  1. public function searchAction()
  2. {
  3.     $request = $this->getRequest();
  4.     $params = $request->getParams();
  5.    
  6.     $this->view->data = $this->_mapper->search($params);
  7.     $this->view->searchForm = $this->_searchForm->populate($params);
  8.    
  9.     $this->render('index');
  10.    
  11. }
このエントリをはてなブックマークに追加このエントリをdel.icio.usに追加このエントリをLivedoor Clipに追加このエントリをYahoo!ブックマークに追加このエントリをFC2ブックマークに追加このエントリをNifty Clipに追加このエントリをPOOKMARK. Airlinesに追加このエントリをBuzzurl(バザール)に追加このエントリをChoixに追加このエントリをnewsingに追加

Comments:0

Comment Form
Remember personal info

Trackbacks:0

Trackback URL for this entry
http://tenderfeel.xsrv.jp/php/zend-framework/1050/trackback/
Listed below are links to weblogs that reference
[ZF] Zend_Form : Remove the submit-button value from url parameters from WebTecNote

Home > Zend Framework > [ZF] Zend_Form : Remove the submit-button value from url parameters

最近の投稿
最近の修正
  • そしてこのSQLはわれながらよく書いたと思う 2010-11-15
  • CSVの列っていう方がいいのかな…118項目だった 2010-11-15
  • 楽天のCSVの項目が116個もあった衝撃 2010-11-15
  • オフィスで香水臭振りまくのは迷惑だと知れ 2010-11-15
  • ぐあー フレグランステロやー 2010-11-15
  • More updates...

Powered by Twitter Tools

Tag Cloud
おすすめサーバー・他
メタ情報

Return to page top