Daniel Bingham
Daniel Bingham

Reputation: 12914

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 not 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.

Upvotes: 1

Views: 7770

Answers (4)

LittleBigDev
LittleBigDev

Reputation: 474

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
));

Upvotes: 3

Tomáš Fejfar
Tomáš Fejfar

Reputation: 11217

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?

Upvotes: 0

A.J. Brown
A.J. Brown

Reputation: 932

Zend_Form_Element_MultiCheckbox is what you're looking for.

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

Upvotes: 1

DarkLeafyGreen
DarkLeafyGreen

Reputation: 70406

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.

Upvotes: 1

Related Questions