Zend Form: Checkbox element displays as hidden field?

后端 未结 6 1870
春和景丽
春和景丽 2021-02-20 08:34

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\\\         


        
6条回答
  •  自闭症患者
    2021-02-20 09:13

    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.

提交回复
热议问题