Welcome Guest, Not a member yet? Register   Sign In
Form Validation Class - Get validated values?
#1

[eluser]dmorin[/eluser]
How do you get the validated values from the new form validation class? For example, if I want to save the result to the database, how would I get the values for the queries? The old validation class created class vars for each field but this one doesn't seem to do that, or am I wrong?

I swear I can't find it in the manual.

Edit:
So, it looks like the preferred way is to just reference $_POST or $this->input->post() which seems odd since it destroys the original input, but regardless, might be a good thing to mention in the form_validation page of the user guide.

So my new problem is that apparently, when it repopulates $_POST with the validated data, it checks to see if that key already exists in the $_POST array. This breaks my app since I'm using a callback function to set a value in case that field doesn't exist. It seems as though if the Form_Validation class bothers to validate a field that isn't posted, it should probably repopulate a field that isn't posted.

If anyone thinks I'm doing this wrong, please let me know how you would do it. The other solution is to change this:
Code:
if (isset($_POST[$row['field']]))
{
    $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
}
to
Code:
//if (isset($_POST[$row['field']]))
//{
    $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
//}

Please let me know if you are aware of this breaking anything. Thanks.
#2

[eluser]Colin Williams[/eluser]
There is a set_value() function that returns what you want.
#3

[eluser]dmorin[/eluser]
Yeah, I knew about set_value(), but I thought that also escapes the value for being used as the value of a form element, which is really not what you want for adding to a database or doing anything else with for that matter.
#4

[eluser]Colin Williams[/eluser]
Then using $this->input->post() is the way to go. I would revisit what your callback is doing since you say it causes problems.
#5

[eluser]dmorin[/eluser]
My callback simply sets a value if one isn't currently set. It seems like I CI bug to me. Can a moderator move this to the bugs forum please?
#6

[eluser]Colin Williams[/eluser]
What is the bug? Spell it out.
#7

[eluser]dmorin[/eluser]
If you define a field to be validated using the form validation library and it isn't included in the $_POST array, the form validation library will still validate it including running any callback functions, but will not add the processed value back into the $_POST array at the completion of the validation.

Further, since the set_value() function DOES correctly output the field (though form encoded?), it seems as though the validation library is acknowledging that it's valid, which seems to indicate that it be available through the accepted way to retrieving the validated information
#8

[eluser]Colin Williams[/eluser]
Quote:If you define a field to be validated using the form validation library and it isn’t included in the $_POST array

Here's your first problem. Under what circumstance should this happen, other than a poorly written piece of functionality?

Quote:..but will not add the processed value back into the $_POST array

How can there be a "processed value" when no value originally existed?

Quote:Further, since the set_value() function DOES correctly output the field..

This only happens because your callback invented the $_POST value. Furthermore, it prep_for_form()'d it when it did so.
#9

[eluser]Colin Williams[/eluser]
Changing

Code:
$_POST[$row['field']] = $this->prep_for_form($row['postdata']);

to

Code:
$_POST[$row['field']] = $row['postdata'];

might solve your problem. Although I don't see why you are injecting data into the $_POST array in the first place.
#10

[eluser]CroNiX[/eluser]
Also, you don't need to use a special callback to set the default value, that's what the 2nd parameter of set_value() is for Smile




Theme © iAndrew 2016 - Forum software by © MyBB