2

Could you please help me to solve my problem?
I would like to place a checkbox on every row of a list of users built with database information and add a validation button to post my form. My list should look something like this:-

Diagram of form

So the user will select the checkbox linked to the student he wants to validate. The number of rows of the result is variable, so i don't know how to do it. I hope i've been clear in my description.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
jojo7
  • 21
  • 3
  • There is no such thing as a multicheckbox. A checkbox is either set or it's not. Just create a row for each row in your database and add a unique checkbox for each. – markus Feb 26 '12 at 20:59
  • thx Markus!please tell me how to add a unique checkbox for each row dynamically.I used to use zend form to add forms in my application (when i already know the number of checkbox to add).This time the number of checkbox is function of the number of rows that the database find. Must i add it in my view file directly?if yes how? – jojo7 Feb 26 '12 at 21:24
  • You don't have to do it in your view, you can do it in your form file. Just do a foreach loop over the result array. – markus Feb 26 '12 at 21:30
  • could you please give me an example?i don't understand what you mean – jojo7 Feb 26 '12 at 23:19
  • Check out http://stackoverflow.com/questions/8219988/zend-form-elements-in-a-table-containing-also-data-from-the-database/8223430#8223430 – David Weinraub Feb 27 '12 at 02:08

2 Answers2

0

You haven't shown any code, but I am assuming that you get the number of students somewhere in your controller.

To achieve what you want with Zend_Form, you will need to render each element individually, but first you need to find a way of adding the correct number of elements to your form.

Preferably, you would do this in your form class to keep the logic out of the controller, but to keep this answer simple I will show you how to achieve this in your controller, you can then adapt the code as you wish.

$numStudents = getNumberOfStudentsSomehow();
$studentForm = new yourFormClass();

for($i = 0; $i <= $numStudents; $i++){
    $checkBoxes[] = new Zend_Form_Element_Checkbox('checkBox_' . $i);
}
$studentForm->addElements($checkBoxes);
$this->view->studentForm = $studentForm;

Your form now has the correct number of check boxes in it and you can pass it to the view.
In the view you have several options for rendering the form, either a view partial as suggested by RockyFord, a view helper (documentation here), create a custom view script for your form, or render directly in your view.

To get you started you can render individual elements from your form in your view like this:-

echo $this->view->studentForm->checkBox_0;
Community
  • 1
  • 1
vascowhite
  • 18,120
  • 9
  • 61
  • 77
-1

I think this might be a situation were a partialLoop() might be the best solution.

in your controller get your data from the model as usual and assign it the data to the view

$this->view->modelData= $data;

next make a new .phtml file in /views/scripts for this demo we'll call it _demoRow.phtml then code the html and php for one table row (in this case).

<tr>
    <td><?php echo $this->name ?></td>
    <td><?php echo $this->class ?></td>
    <td><?php echo $this->birth_date ?></td>
    <td><input type="checkbox" name="id" value="<?php echo $this->id ?> /></td>
</tr>

Then in your normal view just put the static information and render the partial

<form action="/your/action/url" method="post">
  <table class="spreadsheet" cellspacing="0">
    <tr>       
        <th>Student Name</th>
        <th>Class</th>
        <th>Birth Date</th>
        <th>Select</th>
    </tr>
    <?php echo $this->partialLoop('_demoRow.phtml', $this->modelData) ?>
    <tr>
        <input type="submit" name="submit" value="valider" />
    </tr>
  </table>
</form>

This should approximate what you're looking.

RockyFord
  • 8,529
  • 1
  • 15
  • 21
  • What happens when the form is submitted though? Zend-Form will not know about the new elements and they won't be available to it. – vascowhite Feb 27 '12 at 08:26
  • @vascowhite when you hit the submit button the form submits with a post method just like any other form. Zend_Form is not magic, it just abstracts the building of forms and generates html it has nothing to do with what happens when a form is posted. – RockyFord Feb 27 '12 at 08:59
  • Some methods of Zend_Form will not work with your solution:- `isValid()`, using `foreach()` to step through elements, `getValues()`, `getValue()` on any of the added elements, `getValidValues()`; all of which work on the submitted form. There are others, but I think that's enough to illustrate my point. – vascowhite Feb 27 '12 at 11:50
  • @vascowhite foreach() will work with any array, isValid belongs to Zend_Validate and getValues and getValue() just apply Zend_Filter_Input to getParams() or getPost(). Granted Zend_Form is convenient but it's not required and sometimes makes things more difficult. – RockyFord Feb 27 '12 at 12:10
  • The OP has said (implied?) he is using Zend Form. All the methods I have cited belong to [Zend Form](http://framework.zend.com/apidoc/1.11/db_Form.html#%5CZend_Form), I took the examples directly from the code. Your solution is valid, I only challenge it because of the OP's reference to Zend Form. looking at your profile, it seems your background is similar to mine :) – vascowhite Feb 27 '12 at 12:42
  • @vascowhite agreed, I was just presenting an option. Let's face it there are any number of ways to skin this cat, and now between you, me and David Wienraub he has three different approaches to think on. – RockyFord Feb 28 '12 at 04:51