Welcome Guest, Not a member yet? Register   Sign In
Form Validation and repopulating the form (set_value)
#1

[eluser]Unknown[/eluser]
Hello. I am new to CI, and am finding that I like it very much. At the moment, I am just getting a feel for how all of it works. However, I have run into a problem that I was wondering someone could assist me with.

Here's the scenario:
I query the DB for a record, set the values in an array, and then render the view with this array. Upon form submission, I run a form validation on it. If form validation fails, I re-render the view.

In the view, I am using the form helper's function set_value to repopulate the form in the event the form fails validation. The problem is if I have a field that I don't need to validate (an optional free-form field for example), the value the user entered when they submitted the form does not repopulate in the event of a form validation error.

It appears the set_value function in the form helper class first checks to see if the form validation library is loaded. If it is, it dispatches control to that class to get the value of the field. But if that field didn't have any validation set on it, the form validation class knows nothing of it.

It seems the form helper should also be checking if the form validation library contains the field in question, and if it doesn't, it should use the $_POST array.

Any ideas?

Thanks!
#2

[eluser]jrtashjian[/eluser]
Hey, I have run into this same exact problem quite often.

What I have done to re-populate fields that are not required is to just set a rule on the field and leave the rule as an empty string. Like so:

Code:
$this->form_validation->set_rules('form_element', 'Form name', '');

instead of this:

Code:
$this->form_validation->set_rules('form_element', 'Form name', 'required');

If you leave the rule blank, the field does not get validated but will be re-populated when you use set_value('form_element')

Hope that helps! I know this is a late response Smile
#3

[eluser]TheFuzzy0ne[/eluser]
[quote author="puppycrack" date="1231742124"]It seems the form helper should also be checking if the form validation library contains the field in question, and if it doesn't, it should use the $_POST array.
[/quote]

I'm not sure I understand. That's exactly what the helper does do. If neither exist, then it's up to you to supply a default value for set_value().

Besides, in my very limited experience, I've yet to find any user submitted data that doesn't require validation of some kind. There usually something to be checked, even if it's just the length to ensure it'll fit in the database.
#4

[eluser]TheFuzzy0ne[/eluser]
I made a mess of my last post, so allow me to re-iterate on the things that were a bit fuzzy®.

The form helper class is just an extension of $this->form_validation->set_value().

The form validation library works directly on $_POST, so really, set_value only relies on the post variable having been set to return a value. If the post variable hasn't been set, a default is used if you've supplied one.

That makes more sense to me now, so hopefully it also makes sense to you to.
#5

[eluser]jrtashjian[/eluser]
Here is an example, you need to create a form which is used to gather the users address. Fields will include:

- First Name
- Last Name
- Address Line 1
- Address Line 2
- City
- State/Province
- Zip/Postal Code
- Country

I would require all fields except 'Address Line 2' to be filled in. I would set a rule for each field:
Code:
$this->form_validation->set_rules('first_name', 'First Name', 'required');
$this->form_validation->set_rules('last_name', 'Last Name', 'required');
$this->form_validation->set_rules('address_line_one', 'Address Line 1', 'required');
$this->form_validation->set_rules('city', 'City', 'required');
// etc...

Then for each form element on the view I would place set_value('first_name'), set_value('last_name'), etc... for all form elements (including address_line_two).

Upon following the code of the form validation library, only fields that you have declared in set_rules() are ever checked for post values. If it has not been declared, no validation happens, and set_value() will no re-populate.

However, I did notice that this only happens when the form validation library and the form helper is loaded. If only the form helper is loaded, it re-populates the data. I don't believe this is a bug. To me, the form validation library is doing exactly what it's supposed to be doing. I think it has to do with the form helper set_value() method.




Theme © iAndrew 2016 - Forum software by © MyBB