开发者

How do I get a regular Checkbox in a Zend Form?

I have a form in Zend_Form that needs some checkboxes and I'd like them to be regular old checkboxes. You know, you give em a name and a value. If they are checked your post data contains name=>value.

Zend_Form is generating two inputs fields. One, the checkbox with a value=1 and the second a hidden input with a value=2. Both have the same name. I understand in theory how Zend expects the checkbox to work, but that's no开发者_如何学编程t how I expect it to work and it's not how I want it to work. How do I get my old fashion HTML checkbox back?

I have tried using $this->createElement, $this->addElement and creating a Zend_Form_Element_Checkbox manually. None allow me to set the checkbox's value and all generate the hidden input.


The final and REALLY correct answer is to add an option to the element :

$this->addElement('checkbox', 'my_element', array(
    'label' => 'My Element Label',
    'name' => 'my_element_name',
    'disableHidden' => true
));


Zend_Form_Element_MultiCheckbox is what you're looking for.

The standard Checkbox element is meant to represent "yes/no" scenarios.


You could extend Zend library and add your own custom form element to render it just like you expect it. I did it for having a date field and it worked just fine.


I wonder why that does not work for you. You can set the values to anything you want (setCheckedValue() and setUncheckedValue()). So the only difference to normal checkbox is

if (null == $this->_getParam('checkbox', null)) {
//vs.
if ($unchecked == $this->_getParam('checkbox')) {

What exactly are you trying to do?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜