Zend Framework: how to create a Zend_Form (sub form) as table of checkboxes with empty and text-only cells that aren't submitted?
How do you add empty cells and cells that contain only text to a table on a Zend_Form, that otherwise only contain checkboxes (table wrapped onto a form!), so that their values aren't submitted?
I've basically followed this tutorial and indeed my code isn't that much different in structure:
http://davidcaylor.com/2008/03/24/building-table-based-forms-in-zend_form/
The big difference is that I need a checkbox per td tag, some text, or nothing inside. As I have no idea how to add just text or "empty content" to such a ZF sub form, I have added a disabled hidden input to these table cells using:
// empty cells
$form->addElement('hidden', $name, array('disabled' => 'disabled'));
// cells with descriptive text only
$form->addElement('hidden', $name, array('disabled' => 'disabled', 'label' => $name));
This shows the table of checkboxes produced by a Zend_Form and its decorators, some cells text-only, and some empty:
<table><tbody>
<tr>
<td>
<input type="hidden" name="checkboxes[isoansi][isoansi_dbms]" value="" disabled="disabled" id="checkboxes-isoansi-isoansi_dbms" />
<label for="checkboxes-isoansi-isoansi_dbms" class="optional">ISO/ANSI SQL</label></td>
<td>
<input type="checkbox" name="checkboxes[isoansi][isoansi_all]" id="checkboxes-isoansi-isoansi_all" value="1" checked="checked" />
<label for="checkboxes-isoansi-isoansi_all" class="optional">All</label></td>
<td>
<input type="checkbox" name="checkboxes[isoansi][isoansi_1992]" id="checkboxes-isoansi-isoansi_1992" value="1" checked="checked" />
<label for="checkboxes-isoansi-isoansi_1992" class="optional">1992</label></td>
<td>
<input type="checkbox" name="checkboxes[isoansi][isoansi_1999]" id="checkboxes-isoansi-isoansi_1999" value="1" checked="checked" />
<label for="checkboxes-isoansi-isoansi_19开发者_Python百科99" class="optional">1999</label></td>
<td>
<input type="checkbox" name="checkboxes[isoansi][isoansi_2003]" id="checkboxes-isoansi-isoansi_2003" value="1" />
<label for="checkboxes-isoansi-isoansi_2003" class="optional">2003</label></td>
<td>
<input type="checkbox" name="checkboxes[isoansi][isoansi_2008]" id="checkboxes-isoansi-isoansi_2008" value="1" checked="checked" />
<label for="checkboxes-isoansi-isoansi_2008" class="optional">2008</label></td>
<td>
<input type="hidden" name="checkboxes[isoansi][isoansi_4_empty]" value="" disabled="disabled" id="checkboxes-isoansi-isoansi_4_empty" /></td></tr>
... more table rows
<tr>
<td>
<input type="hidden" name="checkboxes[sqlserver][sqlserver_dbms]" value="" disabled="disabled" id="checkboxes-sqlserver-sqlserver_dbms" />
<label for="checkboxes-sqlserver-sqlserver_dbms" class="optional">SQL Server</label></td>
<td>
<input type="checkbox" name="checkboxes[sqlserver][sqlserver_all]" id="checkboxes-sqlserver-sqlserver_all" value="1" checked="checked" />
<label for="checkboxes-sqlserver-sqlserver_all" class="optional">All</label></td>
<td>
<input type="checkbox" name="checkboxes[sqlserver][sqlserver_2000]" id="checkboxes-sqlserver-sqlserver_2000" value="1" />
<label for="checkboxes-sqlserver-sqlserver_2000" class="optional">2000</label></td>
<td>
<input type="checkbox" name="checkboxes[sqlserver][sqlserver_2005]" id="checkboxes-sqlserver-sqlserver_2005" value="1" checked="checked" />
<label for="checkboxes-sqlserver-sqlserver_2005" class="optional">2005</label></td>
<td>
<input type="checkbox" name="checkboxes[sqlserver][sqlserver_2008]" id="checkboxes-sqlserver-sqlserver_2008" value="1" checked="checked" />
<label for="checkboxes-sqlserver-sqlserver_2008" class="optional">2008</label></td>
<td>
<input type="hidden" name="checkboxes[sqlserver][sqlserver_3_empty]" value="" disabled="disabled" id="checkboxes-sqlserver-sqlserver_3_empty" /></td>
<td>
<input type="hidden" name="checkboxes[sqlserver][sqlserver_4_empty]" value="" disabled="disabled" id="checkboxes-sqlserver-sqlserver_4_empty" /></td></tr>
</tbody></table>
As you can see, the hidden inputs are all disabled (text-only cells to the left and some empty ones to the right of the table).
However, when I submit the form and print all postback values, the disabled hidden inputs of the empty cells and the text-only cells also appear in the $_POST list, even though they're supposed not to be submitted (this is what disabled="disabled" is for!).
What's wrong here?
Again, in the HTML output all empty and text-only cells have disabled="disabled" set.
I have two possible solutions now:
Either add something else but labeled, disabled hidden inputs to the table data (td) cells (Zend_Sub_Form) OR
Find out why the disabled hidden inputs are submitted
Can anybody help? What could be going wrong?
If you use $form->getValues() method to retrieve submitted data, the disabled fields are also returned. If you want Zend_Form to ignore these values try setting ignore property, e.g.
$form->addElement('hidden', $name, array('disabled' => 'disabled','ignore'=>true));
精彩评论