Welcome Guest, Not a member yet? Register   Sign In
set_checkbox function and dynamic forms
#1

[eluser]Coen de Jong[/eluser]
Hi Guys,

I have a form that is generated based on fields that are stored in the database. So the form is generated dynamicly.

Now what I want to do is set the checkboxes in the form after validation went wrong, with the set_checkbox helper. I just can't get it working though. Maybe you have any idea?

The generation of the form goes as follows:

Code:
<p class="nomargin">
&lt;input
&lt;?= set_checkbox("sector[".$row-&gt;parentsector_ID."][]", $row->sector_ID) ?&gt;
type="checkbox"
id="sector_&lt;?= $row->parentsector_ID ?&gt;_&lt;?= $row->sector_ID ?&gt;"
name="sector[&lt;?= $row->parentsector_ID ?&gt;][]"
value="&lt;?= $row->sector_ID ?&gt;" />
<label for="sector_&lt;?= $row->parentsector_ID ?&gt;_&lt;?= $row->sector_ID ?&gt;">&lt;?= $row->sectorname ?&gt;</label>
</p>

The html that is generated from that is:

Code:
<p class="nomargin">&lt;input  type="checkbox" id="sector_1_14" name="sector[1][]" value="14" /&gt; <label for="sector_1_14">Champignonteelt</label></p>

The post array that is generated when some of the items are selected:

Code:
Array
(
    [1] => Array
        (
            [0] => 14
            [1] => 11
            [2] => 13
        )

    [2] => Array
        (
            [0] => 25
            [1] => 22
        )

    [3] => Array
        (
            [0] => 33
            [1] => 34
        )

    [5] => Array
        (
            [0] => 51
        )

    [6] => Array
        (
            [0] => 61
        )

)

But even when a checkbox is actually checked, it doesn't get repopulated after submission. Why not? What am I doing wrong?
#2

[eluser]LuckyFella73[/eluser]
In the set_checkbox section you have:
Code:
$row->sector_ID

I guess when you first populate your form from database
this value is available but not after validating anymore,
since this value comes from your db.
Try to get the value from checkbox after submitting and
pass as a "normal" value. Of course you then have to
edit your controller a little bit in the section where
you first populate the db values in your form.

Roughly this way:

# populate first time:
1. get data from db
2. assign to your data array while looping your query:
Code:
$data['checkbox_x'] = $row->checkbox_x;
$data['parentsector_ID'] $row->parentsector_ID;
// in your form in the set_checkbox section:
Code:
set_checkbox("sector[".$parentsector_ID."][]", $checkbox_x)


3. in the validation part assign sended value again to the same $data array
Code:
$data['checkbox_x'] = $this->input->post('sector_whatever');
(maybe you have to set up a hidden input field for the $parentsector_ID value )

That should work
#3

[eluser]Coen de Jong[/eluser]
Hi,

Thanks for the fast answer. I use the same view for the first generation and validation of the form, and in both cases is the database result set available. I just pass the result set to the view both times:

Code:
$data = $this->model->get_sectors();
$this->load->view("form", $data);

So it should be available after validation, right?
#4

[eluser]LuckyFella73[/eluser]
But if you set the values before AND after submitting
regarding to the allready existing database entries
the checkbox values cant't be repopulated correctly.
The values will stay allways like in db regardless
what the user checks or not.
#5

[eluser]Coen de Jong[/eluser]
I'm sorry, but I don't follow... It is true I use the same view for the filling in part and for the validation part if anything was not valid, but that should not change anything.

As far as I'm concerned, the post array is there and CI checks whatever he finds in the post array against the set_checkbox values I provide. If the post array says I checked the sector box with value '47', and I say set_checkbox('sector[]', 47), he needs to echo "checked". Regardless of what is in the database. The link with the database is only needed to retrieve the different possible sectors, not to determine whether or not they were selected.
#6

[eluser]LuckyFella73[/eluser]
You don't have to be sorry - I was in a hurry answering
to your post and mixed it up with repopulating input fields,
where you would have to go the way I said (at least simular).

So I'm sorry for posting without thinking enough about that ...
#7

[eluser]Coen de Jong[/eluser]
Ok no problem Smile

I figured it out though! It is perfectly possible to use the repop functions even with dynamic data. But the trick seems to be that there _has_ to be a rule attached to the input element or otherwise CI doesn't recognize it. And since I had a bunch of checkbox input elements that didn't need any rule (they were completely optional), somehow they can't be repopulated accordingly. Even though there are other elements in the form that _can_ trigger an error.

Furthermore I discovered that the second argument of set_checkbox (and _radio too) needs to be given as string. When given as int they are interpreted wrong.

So, bottomline: _always_ make a rule for repopulating form elements, even if there is nothing to validate:

Code:
$this->form_validation->set_rules("element[]", "Human name");

And always be sure to pass arguments to repop functions as strings:

Code:
set_checkbox("element[]", "1");  //correct
set_checkbox("element[]", 1);    //wrong




Theme © iAndrew 2016 - Forum software by © MyBB