Issues

ZF-1666: Zend_View - form helpers produce extraneous hidden elements

Description

Several form helpers produce extraneous hidden elements (such as formCheckbox).

If the developer chooses to produce hidden elements, they should do so with formHidden(), having _hidden automatically implied makes for hard-to-debug xhtml / js / form elements.

Furthermore, while not completely sure at the moment, I dont think this meshes well with the future of xhtml, specifically when it comes to the name and id attributes.

More notes later.

Comments

This really should be resolved/fixed, as it really jacks stuff up like checkboxes and radio buttons

Fix version after 1.0.1.

Scheduled for 1.0.3

Actually, I don't have enough information to close this. I need the following:

  • Show some code using the helpers, the XHTML generated
  • Detail what the mis-behaviour is, and what you'd like to see

I spoke with Paul M. Jones regarding this, and he put in the hidden elements originally to aid browsers that don't have JS -- so that if no option is selected, a value is still returned to the handler (i.e., so the form element key is populated on the server side). This seems like a reasonable goal, but I need to know if it conflicts with HTML specs and/or JS libraries.

thanks!

I am running into trouble with this useless hidden field in Internet Explorer. IT breaks my JS validation since it generates an empty value where none should be. This empty value prevents my forms to be submitted. But only in IE.

Here is the generated HTML. AngekÞndigt
Aktiv
Abgelaufen

I do a standard validation for empty values using JS:

/** * Throws an alert if any of the mandatory field is empty * * The param mandatory_fields contains the id's of all mandatory fields. * * @author Ralf Kramer * @param string mandatory_fields: a list of mandatory form fields seperated by a whitespace * @return boolean */ function hasEmptyFields( mandatory_fields, message ) { field_array = mandatory_fields.split( " "); empty_fields = ""; for( i = 0; i < field_array.length; i++ ) { var field = document.getElementById( field_array[i] ); if( field.value == "" ) { field.style.border = '1px dotted red'; empty_fields = empty_fields + field_array[i]; } else field.style.border = '1px dotted green'; }

if( empty_fields.length > 2 )
{
    alert( message );
    return true;
}

return false;

}

Sure it would be maybe possible to check the type, and if its a radio or a checkbox, the JS could ignore empty fields, but this is not the intention. My explicit intention is to avoid empty fields. If I want to populate a value on the server and really need that hidden field, I can easily create it on my own. But this entire usecase appears to me kind of "constucted"...

Adding patches for FormCheckbox and FormRadio that allow you to choose between generating hidden elements or not, without breaking BC.

Here is my solution with ZF 1.0.2 (applied to my AJAX project) :

Let's see what FormRadio does... ... // build the element if ($disable) { ... } else { ... // is it checked? if ($opt_value == $value) { $radio .= ' checked="checked"; $xhtml = $this->_hidden($name, $value); //my modification, let hidden form has value } ... } ...

(You may find out those code in Zend/View/Helper/FormRadio.php)

Then copy it to ur helper path, change name, add the modification, and put some extra js code to make hidden form value to be updated while radios clicked.

So, when you want to validate it, you only have to check the value of hidden form.

I always assign default value to formRadio, and if u don't do that, you may need to assign value to hidden form while no radio is checked

Resolved in trunk

The problem still exists in ZF 1.10.1 AND 1.10.3.

Anyway: is this behavior W3 compliant?

I cant reproduce it too. Please could somebody answer this? My checkbox value is not being saved correctly when i user a different checkedValue and unCheckedValue options.

This behavior is still present in ZF version 1.11.2. Please reopen it.

For those indicating they are seeing issues, could you please provide the following?

  • Small sample code exhibiting the behavior
  • Actual output
  • Expected output

We currently emit hidden values for checkboxes to ensure we get some value on the server side; otherwise, unchecked boxes are not included in the GET and/or POST, which can lead to "missing" values in the form -- often resulting in false negatives when validating.