associated array elements in zend form
Been trying to find a solution for this for a while with not much luck...
Need to render a form with an array of checkboxes each having an associated textbox.
given an array of array('Dave'=>23,'Pete'=>12,'Si'=>43);
the resulting mark-up should yield:
<div>
<label><input type="checkbox" name="user_id[]" id="user_id-1" value="23" />Dave</label>
<label for="job-1">Job:</label><input type="text" name="job[]" id="job-1" />
</div>
<div>
<label><input type="checkbox" name="user_id[]" id="user_id-2" value="12" />Pete</label>
<label for="job-2">Job:</label><input type="text" name="job[]" id="job-2" />
</div>
<div>
<label><input type="checkbox" name="user_id[]" id="user_id-3" value="43" />Si</label>
<label for="job-3">开发者_JS百科Job:</label><input type="text" name="job[]" id="job-3" />
</div>
Complete zend noob so any help appreciuated (including decorators etc.)
Thanks peeps
Can't be done without custom elements. I'd suggest watching http://www.zendcasts.com/writing-composite-zend_form-elements/2010/03/
just create a custom decorator, extends from Zend_Form_Decorator_Abstract and define the function render, which returns the html that you define inside, for example you can do:
$html = ''; // some code html
$i = 0;
foreach ($element->getMultiOptions() as $value => $label){
$html .= '<label><input type="checkbox" name="'.$element->getName().'[]" id="'$element->getName()'-'.$i.'" value="'.$value.'" />'.$label.'</label>';
$i++;
}
return $html;
精彩评论