Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend_Form : data table with checkboxes

I'd like to have a table of data coming from the DB in my form element, looking like the following :

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

It would allow to select several rows, like the MultiCheckBox element.

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 it by hand but using Zend_Form would allow me to populate the form, retrieve the values easily and to have validation. I have other normal elements in my form.

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

Thanks. Ask for more info if needed.

This question seems to be related: Zend_Form: Database records in HTML table with checkboxes

Marc

like image 230
Marc Demierre Avatar asked Aug 11 '11 15:08

Marc Demierre


1 Answers

ok, so this is going to be a longer sort of answer

the Form

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

Controller

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

    // ... processing logics

    $this->view->assign('form', $form);
  }
}

View

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

A couple things are happening here.
I get the prepopulated values out of the form object:

<?php $values = $this->form->getElement('subscribers')->getValue(); ?>

I mark each checkbox as checked or not based on the array above

<?php echo in_array($key, $values) ? 'checked="checked"':''; ?>

EDIT IN RESPONSE TO COMMENT B/C COMMENTS DON'T SUPPORT PRE BLOCKS the

$element->setOptions(

or

$element->setMultiOptions(

only accepts key => value pairs, so anything you want to do outside of key value pairs is going to be a little wierd. If your program allows you could pass another variable to the view, an array that uses the same keys as the multiCheckbox so

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

and then in the foreach in the view use

$this->more_datums[$key]['col_1']
like image 84
Francis Yaconiello Avatar answered Oct 11 '22 21:10

Francis Yaconiello