ZF: GET parameters duplication
As an addition to ZF: how to check GET request?.
I have two input fields and one checkbox. The form is standart.
public function init()
{
$this->setMethod('GET');
开发者_如何转开发$new = new Zend_Form_Element_Checkbox('new');
$new->setLabel('New')
->setOrder(3);
$app = new Zend_Form_Element_Select('app');
$app->setLabel('System')
->setOrder(2)
->setRequired()
->addMultiOptions(array('0' => ' ----------- ') + $applications);
$cat = new Zend_Form_Element_Select('cat');
$cat->setLabel('Theme')
->setOrder(1)
->setRequired()
->addMultiOptions(array('0' => ' ----------- ') + $categories);
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Search')
->setOrder(4);
$this->addElements(array(
$cat, $app, $new, $submit
));
}
1) I do the submit where new is 0: http://site.com/?cat=0&app=0&new=0
2) I do the submit where new is 1: http://site.com/?cat=0&app=0&new=0&new=1
Why the new field has duplication then? I would be good if new=0 is absent if new=1
I think it is because Zend_From creates a hidden input field next to your checkbox. It does it because when checkbox is not checked, by definition, no value associated with the checkbox is being sent to the server. So Zend_Form creates the hidden field to have something sent in case a checkbox is unchecked. However, the drawback is that when the checkbox is checked, two values are sent, i.e. a hidden one and the actual value of checkbox.
Why do you care? It should work just fine?
If you DO care for any reason, you can create My_View_Helper_MyCheckbox that won't create the hidden element and assign it (easily, without juggling wth zend_view_helper prefixes) like this:
$decorators = $form->new->getDecorators();
//see which key is ViewHelper and assing it to $key variable
$decorators[$key] = new My_View_Helper_MyCheckbox();
$form->new->setDecorators($decorators);
But note that can bring some complications - like the checkbox may not validate for unchecked state any many other flaws you can't even think of right now. So consider wisely ;)
Hope it helps ;)
精彩评论