Zend_Form: data table with flags

I would like to have a data table coming from the DB in my form element that looks like this:

+-----+-------------------------+-----------------------+
|     | Number                  | Name                  |
+-----+-------------------------+-----------------------+
| [ ] | 123                     | ABC                   |
+-----+-------------------------+-----------------------+
| [x] | 456                     | DEF                   |
+-----+-------------------------+-----------------------+
| [x] | 789                     | HIJ                   |
+-----+-------------------------+-----------------------+

This will allow you to select multiple rows, for example, a MultiCheckBox.

Here is the kind of markup I would like to have:

<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... -->

I can do this manually, but using Zend_Form will allow me to fill out a form, easily get values ​​and get confirmation. I have other normal elements in my form.

Any idea on how to achieve this with Zend_Form? Maybe a custom element and a decorator?

Thank. If necessary, request additional information.

This question seems to be related: Zend_Form: database entries in checkbox HTML table

Mark

+5
1

ok,

<?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);
  }
}

>

<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 echo in_array($key, $values) ? 'checked="checked"':''; ?>

>

B/C

$element->setOptions(

$element->setMultiOptions(

key = > value, , , . , , , multiCheckbox,

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

foreach

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

All Articles