In Zend I have a custom composite element which consists of two text fields and a checkbox.
This is the view helper:
<?php
class My_View_Helper_WorkplaceFactor extends Zend_View_Helper_FormElement{
protected $html = '';
public function workplaceFactor($name, $value = null, $attribs = null){
$this->html = '';
$factor = $applies = $note = '';
if($value){
$factor = $value['factor'];
$applies = $value['applies'];
$note = $value['note'];
}
$helperText = new Zend_View_Helper_FormText();
$helperText->setView($this->view);
$helperCheckbox = new Zend_View_Helper_FormCheckbox();
$helperCheckbox->setView($this->view);
$checked = 0;
if($applies == "1"){
$checked = 1;
}
$this->html .= '<td><label for="' . $name . '[factor]">Faktor</label></td><td>' . $helperText->formText($name . '[factor]', $factor) . '</td>';
$this->html .= '<td><label for="' . $name . '[applies]">Platí</label></td><td>' . $helperCheckbox->formCheckbox($name . '[applies]', $applies, array('checked' => $checked)) . '</td>';
$this->html .= '<td><label for="' . $name . '[note]">Poznámka</label></td><td>' . $helperText->formText($name . '[note]', $note) . '</td>';
return $this->html;
}
}
My problem is populating the checkbox when the form is not valid and reloads itself. When I check the checkbox and reload, it's okay and it populates. When I uncheck the checked one, it's also okay and displays correctly upon reload. But when I want to check the checkbox AFTER the page has been reloaded, the checked value isn't even in the post request so it doesn't work.
The element's markup is as follows:
<tr id="factor1">
<td><label for="factor1[factor]">Faktor</label></td><td><input type="text" name="factor1[factor]" id="factor1-factor" value="Prach" /></td>
<td><label for="factor1[applies]">Platí</label></td><td><input type="hidden" name="factor1[applies]" value="0" /><input type="checkbox" name="factor1[applies]" id="factor1-applies" value="0" /></td>
<td><label for="factor1[note]">Poznámka</label></td><td><input type="text" name="factor1[note]" id="factor1-note" value="" /></td></tr>
I have wandered around stackoverflow and I think the problem is that the checkbox name has the [] in it but if I don't put them there, the 'applies' checkbox value doesn't belong to the 'factor' values array and so it doesn't populate at all. Do you have any idea how to find a way of this magic circle?