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.

5
  • 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. Commented Feb 26, 2012 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? Commented Feb 26, 2012 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. Commented Feb 26, 2012 at 21:30
  • could you please give me an example?i don't understand what you mean Commented Feb 26, 2012 at 23:19
  • Check out stackoverflow.com/questions/8219988/… Commented Feb 27, 2012 at 2:08

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

Comments

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

6 Comments

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 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.
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 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.
The OP has said (implied?) he is using Zend Form. All the methods I have cited belong to Zend 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 :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.