Zend Framework

Zend_Form_Element_Radio generates non valid html

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.9.5
  • Fix Version/s: 1.9.6
  • Component/s: Zend_Form
  • Labels:
    None

Description

ExampleForm.php
$this->addElement('radio', 'test', array(
            'label'    => 'test',
            'multiOptions' => array('Mr' => 'Mr','Ms' => 'Ms','Miss'
            => 'Miss','Mrs' => 'Mrs'),
        ));
output.html
//generated output
<dt id="test-label"><label disablefor="1" class="optional">test</label></dt>
<dd id="test-element">
 <label for="test-Mr"><input name="test" id="test-Mr" value="Mr" type="radio">Mr</label>
<br><label for="test-Ms">
<input name="test" id="test-Ms" value="Ms" type="radio">Ms</label><br>
<label for="test-Miss">
<input name="test" id="test-Miss" value="Miss" type="radio">Miss</label><br><label for="test-Mrs">
<input name="test" id="test-Mrs" value="Mrs" type="radio">Mrs</label></dd>

<label disablefor="1" class="optional">: 'disablefor' is not valid (X)HTML attribute in any case.

Proposed fix:

Index: FormLabel.php
===================================================================
--- FormLabel.php	(revision 19018)
+++ FormLabel.php	(working copy)
@@ -54,9 +54,13 @@
         }
 
         $value = ($escape) ? $this->view->escape($value) : $value;
-        $for   = (empty($attribs['disableFor']) || !$attribs['disableFor'])
-               ? ' for="' . $this->view->escape($id) . '"'
-               : '';
+        if (empty($attribs['disableFor']) || !$attribs['disableFor']) {
+            $for = ' for="' . $this->view->escape($id) . '"';
+        }
+        else {
+            $for = '';
+            unset($attribs['disableFor']); //Remove $attribs['disableFor'] from array
+        }
 
         // enabled; display label
         $xhtml = '<label'
  1. FormLabel.diff
    19/Nov/09 10:20 PM
    0.6 kB
    Mon Zafra
  2. FormLabelTest.diff
    19/Nov/09 10:20 PM
    0.8 kB
    Mon Zafra

Activity

Hide
Dolf Schimmel (Freeaqingme) added a comment -

What is the outcome you had expected, and how do you propose to solve this (without losing functionality, while generating valid html for other versions)?

Show
Dolf Schimmel (Freeaqingme) added a comment - What is the outcome you had expected, and how do you propose to solve this (without losing functionality, while generating valid html for other versions)?
Hide
Ernest Szulikowski added a comment -

The expected outcome is just
<dt id="test-label">
<label class="optional">test</label></dt>,
instead of
<dt id="test-label">
<label disablefor="1" class="optional">test</label></dt>

as attribute 'disabledfor' is not allowed in any of HTML (and XHTML) flavors.

Show
Ernest Szulikowski added a comment - The expected outcome is just <dt id="test-label"> <label class="optional">test</label></dt>, instead of <dt id="test-label"> <label disablefor="1" class="optional">test</label></dt> as attribute 'disabledfor' is not allowed in any of HTML (and XHTML) flavors.
Hide
Ernest Szulikowski added a comment -

I found problem in Zend_View_Helper_FormLabel.

If 'disableFor' attribute is set (and for radio it is) $for is cleaned to empty string, but disableFor attribute still exists $attribs array, so $this->_htmlAttribs($attribs) produces all attribs for label including disablefor="1". The simple solution could be just to remove array element:

// $for = (empty($attribs['disableFor']) || !$attribs['disableFor'])
// ? ' for="' . $this->view->escape($id) . '"'
// : '';

if (empty($attribs['disableFor']) || !$attribs['disableFor']) { $for = ' for="' . $this->view->escape($id) . '"'; }
else { $for = ''; //Remove $attribs['disableFor'] from array unset($attribs['disableFor']); }

Show
Ernest Szulikowski added a comment - I found problem in Zend_View_Helper_FormLabel. If 'disableFor' attribute is set (and for radio it is) $for is cleaned to empty string, but disableFor attribute still exists $attribs array, so $this->_htmlAttribs($attribs) produces all attribs for label including disablefor="1". The simple solution could be just to remove array element: // $for = (empty($attribs['disableFor']) || !$attribs['disableFor']) // ? ' for="' . $this->view->escape($id) . '"' // : ''; if (empty($attribs['disableFor']) || !$attribs['disableFor']) { $for = ' for="' . $this->view->escape($id) . '"'; } else { $for = ''; //Remove $attribs['disableFor'] from array unset($attribs['disableFor']); }
Hide
Ernest Szulikowski added a comment -

if (empty($attribs['disableFor']) || !$attribs['disableFor']) { $for = ' for="' . $this->view->escape($id) . '"'; }
else { $for = ''; unset($attribs['disableFor']); }

Show
Ernest Szulikowski added a comment - if (empty($attribs['disableFor']) || !$attribs['disableFor']) { $for = ' for="' . $this->view->escape($id) . '"'; } else { $for = ''; unset($attribs['disableFor']); }
Hide
Ernest Szulikowski added a comment -

I found problem in Zend_View_Helper_FormLabel.

If 'disableFor' attribute is set (and for radio it is) $for is cleaned to empty string, but disableFor attribute still exists $attribs array, so $this->_htmlAttribs($attribs) produces all attribs for label including disablefor="1". The simple solution could be just to remove array element:

// $for = (empty($attribs['disableFor']) || !$attribs['disableFor'])
// ? ' for="' . $this->view->escape($id) . '"'
// : '';

if (empty($attribs['disableFor']) || !$attribs['disableFor']) { $for = ' for="' . $this->view->escape($id) . '"'; }
else { $for = ''; //Remove $attribs['disableFor'] from array unset($attribs['disableFor']); }

Show
Ernest Szulikowski added a comment - I found problem in Zend_View_Helper_FormLabel. If 'disableFor' attribute is set (and for radio it is) $for is cleaned to empty string, but disableFor attribute still exists $attribs array, so $this->_htmlAttribs($attribs) produces all attribs for label including disablefor="1". The simple solution could be just to remove array element: // $for = (empty($attribs['disableFor']) || !$attribs['disableFor']) // ? ' for="' . $this->view->escape($id) . '"' // : ''; if (empty($attribs['disableFor']) || !$attribs['disableFor']) { $for = ' for="' . $this->view->escape($id) . '"'; } else { $for = ''; //Remove $attribs['disableFor'] from array unset($attribs['disableFor']); }
Hide
Ernest Szulikowski added a comment -

Sorry for double post.

Anyone can confirm this ?

What shall I do now ?

Show
Ernest Szulikowski added a comment - Sorry for double post. Anyone can confirm this ? What shall I do now ?
Hide
Mon Zafra added a comment -

Added code to unset 'disableFor' attribute if set, attached the patch and test. Please review.

Show
Mon Zafra added a comment - Added code to unset 'disableFor' attribute if set, attached the patch and test. Please review.
Hide
Mon Zafra added a comment -

Changed diffs to svn-generated diffs instead of Netbeans.

Show
Mon Zafra added a comment - Changed diffs to svn-generated diffs instead of Netbeans.
Hide
Craig Slusher added a comment -

Accidentally assigned to myself.

Show
Craig Slusher added a comment - Accidentally assigned to myself.
Hide
Matthew Weier O'Phinney added a comment -

Patches applied to trunk and 1.9 release branch – thanks!

Show
Matthew Weier O'Phinney added a comment - Patches applied to trunk and 1.9 release branch – thanks!

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: