associated array elements in zend form

2020-04-21 05:01发布

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">Job:</label><input type="text" name="job[]" id="job-3" />
</div>

Complete zend noob so any help appreciuated (including decorators etc.)

Thanks peeps

2条回答
beautiful°
2楼-- · 2020-04-21 05:45

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;
查看更多
再贱就再见
3楼-- · 2020-04-21 05:47

Can't be done without custom elements. I'd suggest watching http://www.zendcasts.com/writing-composite-zend_form-elements/2010/03/

查看更多
登录 后发表回答