11

I would like to add a simple check box to my form:

$element = new Zend_Form_Element_Checkbox('dont');
$element->setDescription('Check this box if you don\'t want to do this action.');
$form->addElement($element);

However, this is what the html looks like:

<dt id="dont-label">&nbsp;</dt>
<dd id="dont-element">
    <input type="hidden" name="dontAttach" value="0">
    <input type="checkbox" name="dontAttach" id="dontAttach" value="1">
    <p class="description">Don't attach a bulletin. I only want to send an email.</p>
</dd>

The problem with this is that I'm using jQuery to hide all the DT/DDs that have a label of &nbsp; inside the DT and a hidden element inside the DD (so my html will validate and the hidden elements don't take up space on the page). Is there a way to use a Zend_Form_Element_Checkbox without having to display a hidden input element? I'd rather not mess with my jQuery code to add more caveats, but I will if I have to.

Solution:

Apparently, I can't/shouldn't remove the hidden element before the checkbox element. So here's my jQuery code to hide all the hidden form elements from being displayed on a page:

//fix zf hidden element from displaying
$('input[type=hidden]').filter(function() {
    var noLabel = $(this).closest('dd').prev('dt').html() === '&nbsp;';
    var onlyChild = $(this).is(':only-child');
    if (noLabel && onlyChild) {
        return true;
    }
    return false;
}).each(function() {
    $(this).closest('dd').hide()
           .prev('dt').hide();
});
Community
  • 1
  • 1
Andrew
  • 227,796
  • 193
  • 515
  • 708
  • I have a doubt about what you describe. Are you adding the hidden element, or is Zend that adds it? – apaderno Dec 22 '09 at 01:01
  • kiamlaluno, Zend definitely adds a hidden field before each checkbox field. If the checkbox is checked, the hidden value is not passed. If it's unchecked, the hidden value makes it through. – Derek Illchuk Dec 22 '09 at 02:03

6 Answers6

5

To change the way a form element is rendered, you can use the decorators, which can be modified with

// Overwrite existing decorators with this single one:
$element->setDecorators(array('Composite'));

For a list of all the default decorators, you look at standard decorators; for a list of the decorators used by the form fields, you can see standard form elements.

It seems to me that the hidden form elements is added from Zend with a precise purpose, and removing it (if that is even possible) could cause some problems. My first thought is that Zend uses that hidden form to check if the value has been changed, or to verify if the from has been really generated from Zend (this hypothesis seems less plausible).

apaderno
  • 28,547
  • 16
  • 75
  • 90
  • So I guess the answer is...No, I can't/shouldn't remove the hidden element that ZF puts on the page. – Andrew Dec 22 '09 at 17:11
  • 1
    That is correct; the hidden field is used to return a value when the checkbox has not been selected. – apaderno Dec 22 '09 at 17:32
5

topic is really old, but I found similar problem a few days ago - I create form dynamically, by javascript - clicking (+) adds a row (one hidden input, two checkboxes and two selects) to the displayed form.

I found, when i send _POST to the server, that las row isn't sent corectly - default zeros from hidden are sent, instead of selected checkboxes. The solution, is to set checkboxes as arrays:

$c = new Zend_Form_Element_Checkbox( 'check1' );
$c->setIsArray( true );

In this case, additional hidden input isn't rendered.

PS. Sorry for my english ;)

foxiu
  • 51
  • 1
  • 2
  • 3
    If someone is looking for similar solution but for ZF2: $c= new Checkbox('check1'); $c->setUseHiddenElement(false); – Piotr Uchman Feb 07 '14 at 11:21
1

Andrew, here's how to really hide your hidden form elements:

$element1 = $form->createElement('hidden', 'element1');
$element1
  ->setDecorators(array(
    'ViewHelper',
    array('HtmlTag', array('tag' => 'dd'))
  ));

Now, you don't have to mess with jQuery code to try to shrink those down. And, you don't have to worry about hidden checkbox fields, either.

Derek Illchuk
  • 5,638
  • 1
  • 29
  • 29
  • Well...that doesn't exactly hide a hidden element. That just gets rid of the DT and label tags. The DD still takes up space on a page if there are styles being applied to the DD elements. – Andrew Dec 22 '09 at 17:09
  • Your hidden input field has to be wrapped in some block-level element, to be XHTML-valid. If that's not at issue, remove the array('HtmlTag', array('tag' => 'dd')), right? – Derek Illchuk Dec 22 '09 at 17:44
1

You can add an attribute disableHidden to the checkbox form element which prevents it from adding a hidden field while rendering.

$chk = new Zend_Form_Element_Checkbox('test_checkbox');
$chk->setAttrib('disableHidden', true);
Ilya
  • 11
  • 1
1

I'm just coming across the hidden element for checkboxes myself. I don't have a solution for the post but want to add in my thoughts about the hidden field. This is so if the user does not check the box off a value of 0 is still passed. Otherwise the checkbox is not posted.

kenitech
  • 1,151
  • 12
  • 17
1

The thread is old but none answer is corrent...

I had the same issue, but I have figured it out, so here is the CORRECT answer:

If you don't like your checkbox value posted when it's unchecked and sent some value only when it IS checked - just use this code:

$chk = new Zend_Form_Element_Checkbox('test_checkbox');
$chk->setRequired();
$chk->setUncheckedValue(null);
$chk->setCheckedValue(1);
...

The checked value is 'some_value', BUT when checkbox is unchecked the value is null, so it doesn't validate because 'required' is set. It works for me for now and there is no need to use 'sophisticated' jquery scripts to remove hidden fields, etc.

radzi0_0
  • 1,250
  • 2
  • 14
  • 24