Welcome Guest, Not a member yet? Register   Sign In
Form with array of checkboxes/radio buttons
#1

[eluser]RBrowne[/eluser]
I'm trying to develop a form for storing the engineer distribution for a project. The form I have presently is as follows:
Code:
<h2>Projects &raquo; Add Engineer</h2>
&lt;?php echo validation_errors(); ?&gt;
&lt;?php echo form_open('projects/add_engineers'); ?&gt;
    <table border="0" cellspacing="0" cellpadding="3">
        <thead>
            <tr>
                <th>Engineers</th>
                <th>Design</th>
                <th>Planning</th>
                <th>Construction</th>
                <th>Commissioning</th>
            </tr>
        </thead>
        <tbody>
            &lt;?php foreach ($aEngineers as $aEngineer) { ?&gt;
            <tr>
                <td>&lt;?=$aEngineer['name']?&gt;</td>
                <td align="center">&lt;input type="checkbox" name="design[]" value="&lt;?=$aEngineer['id']?&gt;" &lt;?=set_checkbox('design[]', $aEngineer['id'])?&gt; /&gt;&lt;/td>
                <td align="center">&lt;input type="checkbox" name="planning[]" value="&lt;?=$aEngineer['id']?&gt;" &lt;?=set_checkbox('planning[]', $aEngineer['id'])?&gt; /&gt;&lt;/td>
                <td align="center">&lt;input type="checkbox" name="construction[]" value="&lt;?=$aEngineer['id']?&gt;" &lt;?=set_checkbox('construction[]', $aEngineer['id'])?&gt; /&gt;&lt;/td>
                <td align="center">&lt;input type="checkbox" name="connissioning[]" value="&lt;?=$aEngineer['id']?&gt;" &lt;?=set_checkbox('connissioning[]', $aEngineer['id'])?&gt; /&gt;&lt;/td>
            </tr>
            &lt;?php } ?&gt;
        </tbody>
    </table>
    <p>
        &lt;input type='submit' value='Submit'/&gt;
        &lt;input type='reset' value='Clear'/&gt;
    </p>
&lt;/form&gt;
As you can see the checkboxes are generated based on the engineers array (a simple multidimensional array with Id and Name for each engineer). It produces the form perfectly.

My problem arises when it comes to validating and repopulating the form. The checkboxes are all returned without any being checked, which when there's 20 engineers, that's a lot of checkboxes to recheck.

Why is it that the set_checkbox() function doesn't seem to be working? And as a slight aside, why doesn't the set_radio() function not work in a similar situation?

Any help would be appreciated.
#2

[eluser]obiron2[/eluser]
I'm guessing it doesn't like the first argument being an undefined array element and so it doesn't know which checkbox to update.

Try rewriting the form so that you increment a counter as part of the foreach loop and append the counter number to the field name (i.e. design_01, connisioning_04)

It makes the rule validation a bit harder, but should make the form work.

Are you using 1.7.1 or 1.6.x

Obiron
#3

[eluser]RBrowne[/eluser]
Obiron,

Thanks for getting back to me.

I see what you're saying but as you said the validation will be near on impossible. I'm using 1.7.1.

From what I can see in the userguide it should be possible to use the array as the name, is it perhaps more appropriate to use set_checkbox('design[1]', $aEngineer['id']); and specify the array number?
#4

[eluser]cwt137[/eluser]
@RBrowne - Running 1.7.1, eh? Or do you mean the code from SVN?

@obiron2 - I think there is a bug where if you don't put a validation rule for a field and the form gets repopulated (because it didn't pass the validation test) those fields will be blank. Its nice you posted the view. Could you post the add_projects controller method that has your validation stuff in it? It looks like you have the right syntax for the set_checkbox() function. No need to have something like design[1] like RBrowne suggest.
#5

[eluser]RBrowne[/eluser]
@cwt137: It is indeed 1.7.0.

With regards to controller source, see below:
Code:
function add_engineers() {
        //Adds an engineer to the project
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        
        $aData['aEngineers'] = array(
            array(
                'name' => 'Richard Browne',
                'id' => '18'
                ),
            array(
                'name' => 'Mark Kinnear',
                'id' => '19'
                )
            );
        
        if ($this->form_validation->run() == FALSE) {
            $this->page->start();
            $this->load->view('projects/add_engineers', $aData);
            $this->page->load_submenu(14);
            $this->page->finish();
        } else {
            redirect('projects/view_projects');
        }
    }

There are currently no validation rules, purely because there's nothing to validate. The checkboxes can be left all blank should it be desired. However, if there is a bug in the code, that means for every field that I want to repopulate will need to have a corresponding validation rule that returns true always.

Is there a solution to the bug? Or do I need to validate each field of my entire system?
#6

[eluser]cwt137[/eluser]
If you don't have any validation rules, why even run the validation? The form helper library has a set_checkbox() function in it. Take out the form validation and use the form helper and see if it works.
#7

[eluser]RBrowne[/eluser]
cwt137, thanks for pointing that out, I realised that when I wrote the question. There is a need for the validation, and I have since rectified that, so that it now checks to make sure at least one of each phase is ticked. It now works.

However, my question still remains, do I need to set a rule for every field that I wish to repopulate? Or is there a fix?
#8

[eluser]cwt137[/eluser]
For now, yes. But maybe someone will fix it. I might look into it if I get the time.




Theme © iAndrew 2016 - Forum software by © MyBB