开发者

associated array elements in zend form

开发者 https://www.devze.com 2023-01-03 01:47 出处:网络
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.

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消