Zend Form Hack

Posted by Tully on Fri 15 October 2010

Zend_Form_Element_Radio does not allow you to remove the pre-selected option. You can set it initially to an integer value that is not one of your radio option values.

The problem with this is that when you reload the form it doesn't pass validation, and the integer you set above gets reset to the first element option.

To fix this problem, I wrote a hack you can add just before you give the view script the form object that resets it to an integer value that is not already set. If the user has selected an option that is valid, it will stay populated and not remove this selected option.

Zend_Form_Element_Radio Remove Pre-selected Option

// Zend_Form_Radio has a bug where you cannot remove preselected
// values. The element defaults to the first option. This will
// loop through every radio element and set the value to 3
// if the value is empty. This is a hack which unsets the default value.
foreach ($form->getElements() as $element) {
    if ($element->getType() == 'Zend_Form_Element_Radio') {
        if ($element->getValue() == '') {