开发者

Zend Avoid submit button value in GET parameters in url

I have a form, created with Zend_Form, with method = GET used for searching records with elements as below:

[form] user name [input type="text" name="uname"] [input type="submit" value="Search" name="search"] [/form]

After form is submitted all the GET para开发者_开发知识库meters along with submit button value are appearing in the url.

http://mysite.com/users/search?uname=abc&search=Search

How to avoid submit button value appearing in the url? is custom routing the solution ?


When you create your element, you can simply remove the name attribute that was automatically set at creation

$submit = new Zend_Form_Element_Submit('search')->setAttrib('name', '');

Or inside a Zend_Form

// Input element
$submit = $this->createElement('submit', 'search')->setAttrib('name', '');

// Or Button element
$submit = $this->createElement('button', 'search')->setAttribs(array
(
    'name' => '', 'type' => 'submit',
);


When a form gets submitted, all of its elements with their names and values become a part of a GET / POST - query.

So, if you don't want an element to appear in your GET - query, all you need to do is to create this element without a name. That's probably not the best approach, but since we're talking about the 'submit' element, I guess it doesn't matter that much.

Looking at Zend_View_Helper_FormSubmit helper, you can see that it's creating the 'submit' element and setting its name. So, the possible solution would be to create your own view helper and use it for rendering the 'submit' element instead of the default helper.

You can set a custom helper with

$element->setAttribs( array('helper' => 'My_Helper_FormSubmit') );


Then build your own form element class and remove the name attribute from the element with preg_replace. The beauty of it is, it will not interfere with the other decorators.

So the something like this:

class My_Button extends Zend_Form_Element_Submit
{
    public function render()
    {
        return preg_replace('/(<input.*?)( name="[^"]*")([^>]*>)/', "$1$3", parent::render(), 1);
    }
}


You can remove name attribute for submit button in javascript. jQuery example:

$('input[name="submit"]').removeAttr('name');


In the controller that represents the form's action, redirect to another (or the same controller) only including the relevant params.

Pseudocode:

$params = $this->getRequest()->getParams();
if isset($params['search'])
  unset($params['search']);
  return $this->_helper->Redirector->setGotoSimple('thisAction', null, null, $params);

handle form here

This is basically the same idea as Post/Redirect/Get except that you want to modify the request (by unsetting a parameter) in between the different stages, instead of doing something persistent (the images on that Wiki-page shows inserting data into a database).

If I were you, I would leave it in. IMO it's not worth an extra request to the webserver.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜