Zend_Form:带复选框的数据表

Mar*_*rre 5 zend-framework zend-form

我想在表单元素中有一个来自DB的数据表,如下所示:

+-----+-------------------------+-----------------------+
|     | Number                  | Name                  |
+-----+-------------------------+-----------------------+
| [ ] | 123                     | ABC                   |
+-----+-------------------------+-----------------------+
| [x] | 456                     | DEF                   |
+-----+-------------------------+-----------------------+
| [x] | 789                     | HIJ                   |
+-----+-------------------------+-----------------------+
Run Code Online (Sandbox Code Playgroud)

它允许选择多行,如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... -->
Run Code Online (Sandbox Code Playgroud)

我可以手工完成,但使用Zend_Form可以填充表单,轻松检索值并进行验证.我的表格中还有其他正常元素.

有关如何使用Zend_Form实现这一点的任何想法?也许是自定义元素和装饰器?

谢谢.如果需要,请询问更多信息.

这个问题似乎是相关的:Zend_Form:带有复选框的HTML表格中的数据库记录

Fra*_*llo 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
  }
}
Run Code Online (Sandbox Code Playgroud)

<?php
class MyController extends Zend_Controller_Action
{
  public function myTestAction()
  {
    $form = new Form_MyTest();

    // ... processing logics

    $this->view->assign('form', $form);
  }
}
Run Code Online (Sandbox Code Playgroud)

<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>
Run Code Online (Sandbox Code Playgroud)

<?php $values = $this->form->getElement('subscribers')->getValue(); ?>
Run Code Online (Sandbox Code Playgroud)

<?php echo in_array($key, $values) ? 'checked="checked"':''; ?>
Run Code Online (Sandbox Code Playgroud)

$element->setOptions(

$element->setMultiOptions(

$this->view->assign('more_datums', array('value1' => array('col_1' => 'col_1_val'[, ...])));

$this->more_datums[$key]['col_1']

编辑回应评论B/C评论不支持前块

<?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
  }
}
Run Code Online (Sandbox Code Playgroud)

要么

<?php
class MyController extends Zend_Controller_Action
{
  public function myTestAction()
  {
    $form = new Form_MyTest();

    // ... processing logics

    $this->view->assign('form', $form);
  }
}
Run Code Online (Sandbox Code Playgroud)

只接受key => value对,所以你想要在键值对之外做的任何事情都会有点奇怪.如果您的程序允许您将另一个变量传递给视图,则使用与multiCheckbox相同的键的数组

<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>
Run Code Online (Sandbox Code Playgroud)

然后在视图中使用foreach

<?php $values = $this->form->getElement('subscribers')->getValue(); ?>
Run Code Online (Sandbox Code Playgroud)