Welcome Guest, Not a member yet? Register   Sign In
Loading view with $data and $this->validation->set_fields($fields)
#1

[eluser]vclef[/eluser]
Hi,

I'm trying to create a form which includes some predefined data from the DB into dropdown menus along with text fields, text areas, etc. Then upon submission, the form is validated. If the validation fails, I want it to repopulate the fields. Just very typical form creation and submission.

My problem is this:

When the form is first loaded, I'd have something like this in the controller:
Code:
$data = array('heading' => 'My form');
<snip>
$data['categories']   = $this->CategoryModel->query();
$data['knownVendors'] = $this->KnownVendorModel->query();

$this->load->view('myform', $data);
Then in the view, I'll loop $data to build the dropdown menus for categories and knownVendors. Everything works fine here, until ....

... upon the form submission, I also do (as seen in the user's guide)
Code:
$fields['category'] = 'Catogory';
$fields['knownVendor'] = 'Known Vendor';

$this->validation->set_fields($fields);

Now, if I load view without the 2nd argument which is $data, the form won't build (obviously)
Code:
$this->load->view('myform');
. But if I load the view with $data, set_fields() doesn't re-populate my form.

Since I'm guessing everyone has to be doing the same thing I'm doing, could you tell me how it should be done? Maybe I'm not structuring the code right with CI. Please advise. Thanks.

--Henry
#2

[eluser]vclef[/eluser]
...not seeing how these two things could be related, I started digging thru the CI Loader class and tracing back yet to find out how that did not work for me. So I concluded that it must have been my error and I was right. I forgot to set the value of a couple fields to $this->validation-><field> in the view so they were never populated while the rest of the form was.

I guess football is a little distracting for me today. Smile




Theme © iAndrew 2016 - Forum software by © MyBB