我想在表单元素中有一个来自数据库的数据表,如下所示:
+-----+-------------------------+-----------------------+
| | Number | Name |
+-----+-------------------------+-----------------------+
| [ ] | 123 | ABC |
+-----+-------------------------+-----------------------+
| [x] | 456 | DEF |
+-----+-------------------------+-----------------------+
| [x] | 789 | HIJ |
+-----+-------------------------+-----------------------+
允许选择多行,例如MultiCheckBox元素。
以下是我想要的标记:
<table>
<thead>
<tr>
<th>Select</th>
<th>Number</th>
<th>Name</th>
</tr>
</thead>
<tr>
<td><input type="checkbox" name="subscribers[]" value="1234"></td>
<td>1234</td>
<td>ABC</td>
</tr>
<tr>
<td><input type="checkbox" name="subscribers[]" value="375950"></td>
<td>375950</td>
<td>DEF</td>
</tr>
<!-- and so on... -->
我可以手工完成,但使用Zend_Form可以填充表单,轻松检索值并进行验证。我的表格中还有其他正常元素。
有关如何使用Zend_Form实现此目的的任何想法?也许是自定义元素和装饰器?
感谢。如果需要,请询问更多信息。
这个问题似乎有关:Zend_Form: Database records in HTML table with checkboxes
马克
答案 0 :(得分:5)
表格
<?php
class Form_MyTest extends Zend_Form
{
public function init()
{
$element = $this->createElement('multiCheckbox', 'subscribers');
$element->setOptions(array('value1' => 'label1', 'value2' => 'label2'));
$this->addElement($element);
// ... other elements
}
}
<?php
class Form_MyTest extends Zend_Form
{
public function init()
{
$element = $this->createElement('multiCheckbox', 'subscribers');
$element->setOptions(array('value1' => 'label1', 'value2' => 'label2'));
$this->addElement($element);
// ... other elements
}
}
控制器
<?php
class MyController extends Zend_Controller_Action
{
public function myTestAction()
{
$form = new Form_MyTest();
// ... processing logics
$this->view->assign('form', $form);
}
}
查看
<?php
class MyController extends Zend_Controller_Action
{
public function myTestAction()
{
$form = new Form_MyTest();
// ... processing logics
$this->view->assign('form', $form);
}
}
这里发生了一些事情。
我从表单对象中获取预先填充的值:
<form action="<?php echo $this->form->getAction(); ?>" method="<?php echo $this->form->getMethod(); ?>">
<table>
<thead>
<tr>
<th>Select</th>
<th>Number</th>
<th>Name</th>
</tr>
</thead>
<?php $values = $this->form->getElement('subscribers')->getValue(); ?>
<?php foreach($this->form->getElement('subscribers')->getMultiOptions() as $key => $value) : ?>
<tr>
<td><input type="checkbox" name="subscribers[]" id="subscribers-<?php echo $key; ?>" value="<?php echo $key; ?>" <?php echo in_array($key, $values) ? 'checked="checked"':''; ?>/></td>
<td><label for="subscribers-<?php echo $key; ?>"><?php echo $key; ?></label></td>
<td><label for="subscribers-<?php echo $key; ?>"><?php echo $value; ?></label></td>
</tr>
<?php endforeach; ?>
</table>
<!-- rest of form -->
</form>
<form action="<?php echo $this->form->getAction(); ?>" method="<?php echo $this->form->getMethod(); ?>">
<table>
<thead>
<tr>
<th>Select</th>
<th>Number</th>
<th>Name</th>
</tr>
</thead>
<?php $values = $this->form->getElement('subscribers')->getValue(); ?>
<?php foreach($this->form->getElement('subscribers')->getMultiOptions() as $key => $value) : ?>
<tr>
<td><input type="checkbox" name="subscribers[]" id="subscribers-<?php echo $key; ?>" value="<?php echo $key; ?>" <?php echo in_array($key, $values) ? 'checked="checked"':''; ?>/></td>
<td><label for="subscribers-<?php echo $key; ?>"><?php echo $key; ?></label></td>
<td><label for="subscribers-<?php echo $key; ?>"><?php echo $value; ?></label></td>
</tr>
<?php endforeach; ?>
</table>
<!-- rest of form -->
</form>
我根据上面的数组标记每个复选框是否选中
<?php $values = $this->form->getElement('subscribers')->getValue(); ?>
<?php $values = $this->form->getElement('subscribers')->getValue(); ?>
编辑回应评论B / C评论不支持前块
$element->setOptions(
或
$element->setMultiOptions(
只接受key =&gt;值对,所以你想要在键值对之外做的任何事情都会有点奇怪。如果你的程序允许你可以将另一个变量传递给视图,那么使用与multiCheckbox相同的键的数组如此
$this->view->assign('more_datums', array('value1' => array('col_1' => 'col_1_val'[, ...])));
然后在视图中的foreach中使用
$this->more_datums[$key]['col_1']