Welcome Guest, Not a member yet? Register   Sign In
Losing my mind with form_validation [SOLVED]
#1

[eluser]theK2[/eluser]
I'm new to CI... Setting up a simple registration form with form_validation and running into a problem. I need to call my model before the view is displayed so that I can query the DB and get options/items for a couple of dropboxes on the registration form. BUT if form_validation fails and I load the model before displaying the view, the errors will not display. If I remove the call to load the model, all errors are shown.

Ideas? I've been racking my brain on this and can't figure this one out.

Controller
Code:
if ($this->form_validation->run() == FALSE){
   $this->load->model('registration_model');
   $data['fields_of_study'] = $this->registration_model->get_fields_of_study();
   $this->load->view('public/student/register',$data);    
}else{
   // just for testing - no need to redirect until I get the validation sorted out
   $this->load->view('public/student/register_step2');  
}

Model
Code:
function get_fields_of_study(){
   return $this->db->get('fields_of_study')->result();
}
#2

[eluser]InsiteFX[/eluser]
Try this:
Code:
$this->load->model('registration_model');
$data['fields_of_study'] = $this->registration_model->get_fields_of_study();

$this->load->vars($data);

if ($this->form_validation->run() == FALSE){
   $this->load->view('public/student/register');    
}else{
   // just for testing - no need to redirect until I get the validation sorted out
   $this->load->view('public/student/register_step2');  
}

If that doe's not work then your problem may be in the view code!
#3

[eluser]theK2[/eluser]
[quote author="InsiteFX" date="1328578684"]If that doe's not work then your problem may be in the view code![/quote]

Thank you for the quick response but that also did not work. Same result - it fails but doesn't show any errors. Here is the portion of the view where the errors are being displayed:

Code:
<?php
$args = array('id' => 'register');
echo form_open('register/step1/student',$args);

// if there were form validation errors, display here  
echo validation_errors('<p class="error">');
?&gt;

I mean, it all seems pretty straight forward and simple to me... but it's always the small, simple problems that are the hardest to debug.
#4

[eluser]Aken[/eluser]
Can you be more specific about what is happening? Are you receiving form validation errors or PHP errors, or something else?

Are you positive that your form is failing and not displaying the view in your second step?
#5

[eluser]theK2[/eluser]
[quote author="Aken" date="1328582100"]Can you be more specific about what is happening? Are you receiving form validation errors or PHP errors, or something else?[/quote]

Nothing happens - it displays the same view again - no PHP errors, no validation errors, nothing. The fields are not propagated with content (I am using set_value) and no errors are shown where I have validation_errors(). If I remove the line where I load Membership_model, the errors are displayed in the
Code:
<p class="error">
and the fields that had content in them are reset with the appropriate value.

[quote author="Aken" date="1328582100"]Are you positive that your form is failing and not displaying the view in your second step?[/quote]

Yes, if I put echo ("FAIL!") in the if/then section where it is supposed to reload the view with validation_errors, that displays on the page. If I do a print_r($_POST), I do see the values in the $_POST array, but they are not being set as the value for the fields. Here is an example of one of the fields:

Code:
echo ('<label for="Student_Email">Email Address / Login</label>');
echo form_input('Student_Email',set_value('Student_Email'));
#6

[eluser]theK2[/eluser]
I should add, since I just started this project, that model is basically empty. All it has in it is this:

Code:
&lt;?php

class Registration_model extends CI_Controller {
  
   function get_fields_of_study(){
      return $this->db->get('fields_of_study')->result();
   }

}
#7

[eluser]Aken[/eluser]
LOL. Sorry :-p

Double check the class your model is extending. Wink
#8

[eluser]theK2[/eluser]
[quote author="Aken" date="1328583760"]Double check the class your model is extending. Wink[/quote]

OMG did I seriously do that?! Okay, I need a vacation. These 60-70 hour weeks (seriously) are taking their toll. Wow, what a noob-error. HA! Thanks much to you two guys for pointing out my glaring mistake.

Of course, that fixed it. :red:
#9

[eluser]InsiteFX[/eluser]
Your form validation errors should be outside your form_open!
Code:
// if there were form validation errors, display here  
echo validation_errors('<p class="error">');

$args = array('id' => 'register');
echo form_open('register/step1/student',$args);
#10

[eluser]Aken[/eluser]
[quote author="InsiteFX" date="1328584420"]Your form validation errors should be outside your form_open![/quote]
There are no specific reasons why it can't go inside or outside - it contains no form fields that may affect the form.




Theme © iAndrew 2016 - Forum software by © MyBB