Welcome Guest, Not a member yet? Register   Sign In
Problems with form re-populating functions
#1

[eluser]Galvao[/eluser]
Greetings.

I'm using CI 1.7.1 (upgrading, at least *right now*, is not an option), and I'm having all sorts of problems with the form re-populating functions.

When the form is first loaded (meaning that it wasn't submitted yet) the following bizarre behavior takes place (just to state one example):

I have three radio buttons, all named the same ("Selection"), each one with different values (let's say "foo", "bar" and "baz"). So here's that part of the code:

Code:
<input <?php echo set_radio('Selection', 'foo', TRUE);?> type="radio" name="Selection" id="Selection" value="foo" />
<input <?php echo set_radio('Selection', 'bar');?> type="radio" name="Selection" id="Selection" value="bar" />
<input <?php echo set_radio('Selection', 'baz');?> type="radio" name="Selection" id="Selection" value="baz" />

The *first* radio button should be checked, since it's the default and the form wasn't even submitted yet, but what actually happens is that the *last* radio button comes checked.

Two important informations:

1) Although it's a XHTML error (I'm well aware of that), the problem is not being caused by the fact that all radios have the same value in the id attribute. I know because I've tested it with different, unique IDs.
2) This is just an example. I also have problems with set_value, set_select and so on, meaning that practically all those functions are failing in one level or another.

So I'd like to know:

1) If I'm doing somthing wrong, what is it and how to fix it. Or...
2) If it's a 1.7.1 bug and this will only be solved by upgrading.

Please advise. I'm banging my head in this one.

Thanks,

Galvao
#2

[eluser]Trae R.[/eluser]
I have discovered that unless a form field has a validation rule associated with it that the Form Helper set_ functions will not re-populate the value at all when the form is loaded again.

As a workaround I typically create a rule for every field, even if it's something as simple as trim. I believe xss_filter is another validation rule that should be applied to almost any user entry fields anyway, so getting in that habit would be one solution to the "issue".

Does anyone out there know a way around not having to set a validation rule for every field you want to set_?
#3

[eluser]Galvao[/eluser]
Hi, Trae.

I thought this would make sense, but halfway the work I've realized it doesn't: The problem is occurring when the form is first loaded, meaning there's no submission nor validation taking place yet.

Anyway to test the proposed solution I've applied every validation rule and set_* function and as I've expected the problems remain.

One thing I've didn't mentioned: I'm loading the form and dealing with the submission in different actions on the controller, while the documentation example does it all in one action, but that couldn't be the cause, right? I mean, separating form loading and form submission makes all sense to me...

Thanks.

Galvao
#4

[eluser]Galvao[/eluser]
Problem solved. Since I'm using form loading in one action and form submission in another I had to call the validation rules in the form loading action as well.

Two things are worth noticing, anyway:

1) The bug remains in the case of radio buttons, the last one is always checked, so I had to solve this "manually".
2) CI Team really should work on this, since it doesn't make sense to call validation rules upon loading the form.

Galvao
#5

[eluser]CroNiX[/eluser]
[quote author="Galvão" date="1261309793"]Problem solved. Since I'm using form loading in one action and form submission in another I had to call the validation rules in the form loading action as well.

Two things are worth noticing, anyway:

1) The bug remains in the case of radio buttons, the last one is always checked, so I had to solve this "manually".
2) CI Team really should work on this, since it doesn't make sense to call validation rules upon loading the form.

Galvao[/quote]

In your controller why not check to see if the form was submitted before running validation?
Code:
if($this->input->post('submit'))  //assuming you have your submit button named "submit"
{
    if ($this->form_validation->run() == FALSE)
    {
        //failed
    }
    else
    {
        //passed...
    }
}
Then validation is only run if the form was submitted.
#6

[eluser]Galvao[/eluser]
[quote author="CroNiX" date="1261363400"]In your controller why not check to see if the form was submitted before running validation? Then validation is only run if the form was submitted.[/quote]

Because if I don't include the validation routine upon form loading all form re-populating functions break, as described in the first post.
#7

[eluser]-sek[/eluser]
[quote author="Galvao" date="1261309793"]
1) The bug remains in the case of radio buttons, the last one is always checked, so I had to solve this "manually".[/quote]

I just found I have this problem with 1.7.1 I pasted the example set_radio() from the manual into a simple test page and the last radio is always set. Even with specifying TRUE in the first input.

Could you share your workaround?

Thanks

Steve
#8

[eluser]squarebones[/eluser]
Apparently fixed in 1.7.2: this added to line 804 of system/helpers/form_helper.php

... AND $default == TRUE)
#9

[eluser]-sek[/eluser]
Thanks! Changing that line did the trick until I can roll out CI 1.7.2
#10

[eluser]Galvao[/eluser]
[quote author="-sek" date="1262576017"][quote author="Galvao" date="1261309793"]
1) The bug remains in the case of radio buttons, the last one is always checked, so I had to solve this "manually".[/quote]

I just found I have this problem with 1.7.1 I pasted the example set_radio() from the manual into a simple test page and the last radio is always set. Even with specifying TRUE in the first input.

Could you share your workaround?[/quote]

I actually did a manual workaround which is very specific for our application, sorry.




Theme © iAndrew 2016 - Forum software by © MyBB