Render $_GET to hidden fields view helper
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 | <?php /** * Helper to generate hidden elements from query * * @category Zend * @package Zend_View * @subpackage Helper * @copyright Copyright (c) 2011 Netzelf GbR (http://www.netzelf.de) * @license http://framework.zend.com/license/new-bsd New BSD License */class Zend_View_Helper_FormHiddenQuery extends Zend_View_Helper_Abstract { /** * Build hidden input fields from the current query params ($_GET) * * @param string|array $excludes Array or commaseparated list of top level keys to exclude * @return string */ public function formHiddenQuery($excludes = array()) { $request = Zend_Controller_Front::getInstance()->getRequest(); if (!$request instanceof Zend_Controller_Request_Http) { $e = new Zend_View_Exception('Request must be instance of Zend_Controller_Request_Http!'); $e->getView($this->view); throw $e; } if (!is_array($excludes)) { $excludes = explode(',', (string) $excludes); } $get = $request->getQuery(); $get = array_diff_key($get, array_flip($excludes)); $raw = urldecode(http_build_query($get)); $search = '/[&]?([^&]*)\=([^&]*)/'; $replace = '<input type="hidden" name="$1" value="$2" />'."\n"; $hidden = preg_replace($search, $replace, $raw); return $hidden; } } ?> <!-- Used like this: --> <form action="" method="get"> <?php echo $this->formHiddenQuery('q'); ?> <input type="text" name="q" /> <input type="submit" value="Search"/></form> <!-- With this request: http://example.com/?someArray[foo]=bar&foo=bar&q=hello output will be this:--> <form action="" method="get"> <input type="hidden" name="someArray[foo]" value="bar" /> <input type="hidden" name="foo" value="bar" /> <input type="text" name="q" /> <input type="submit" value="Search"/> </form> |
Comments
You must login before commenting on a snippet. If you do not have an account, please register.
Snippet description
When you don't use a router (I admit that happens rarely) and have a GET-form, you have to carry the other get vars (like controller, id etc.) as hidden form fields to not loose them. This one does that for you.
Snippet details
- Created:
-
Christian Opitz
- Edited:
-
Christian Opitz
- Revision Id:
- 186
- Edit Message:
- Initial Release
- ZF Version
- 1.0.4
- Tags:
- form GET hidden
- Comments:
- 0
- Views:
- 3163
- Points:
- 0 (0 votes)