Welcome Guest, Not a member yet? Register   Sign In
Form validation redirect to same page
#1

[eluser]sqwk[/eluser]
I have a form that is assembled into a variable within the controller, and then passed to the viewfile.

Code:
$searchform .= '<input type="text" name="room_number" value="'.set_value('room_number').'" id="room_number" maxlength="2">';

etc for more elements…

This form submits to the same controller, which checks for $this->input->post and displays the relevant results somewhere further down the page. The form fields are populated again with the previous data with functions like set_value().

So far so good. (It works)

Now I would like to validate some of the input fields…

Code:
$this->load->library('form_validation');        
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_message('is_natural', 'This is not a number.');
$this->form_validation->set_rules('room_number', 'Anzahl R&auml;ume', 'is_natural');

and display the relevant error messages:

Code:
$searchform .= form_error('room_number');

Unfortunately the whole thing falls apart: Form errors are not shown and the form fields are no longer populated with the old entries, not on submitting and not on error (not a natural number) How do I get both to work?
#2

[eluser]Cro_Crx[/eluser]
Have you set the form validation fields ? And run the validation according to the documentation here ==>
Code:
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

??
#3

[eluser]sqwk[/eluser]
Yes. The only bit that is missing is the if statement as it is not needed. (I am redirecting to same page if the form is filled out correctly.)

Code:
if ($this->form_validation->run() == FALSE) {
    $this->load->view('myform');
} else {
    $this->load->view('formsuccess');
}
#4

[eluser]Cro_Crx[/eluser]
but your still running
Code:
$this->form_validation->run();
at some point, yeah ?

If you are and still have problems. Post a snippet of your code within your controller that's having the problems.
#5

[eluser]sqwk[/eluser]
Code:
$this->form_validation->run();

is running. I did a bit of rearranging and used the if statement instead: it looks like the form is being validated, but the errors don't show up and the fields are not re-populated.

Right now I am adding the errors to the same variable as the form, like so:

Code:
$searchform .= '<label for="room_number">Anzahl R&auml;ume</label>';
$searchform .= '&lt;input type="text" name="room_number" value="'.set_value('room_number').'" id="room_number" maxlength="2"&gt;';
$searchform .= form_error('room_number');

where $searchform is passed to the viewfile.
#6

[eluser]oliur[/eluser]
I am on the same boat, would love to see a solution. thanks.
#7

[eluser]Cro_Crx[/eluser]
Is there any reason why your using a PHP variable to store the form information ?
Your application would be more robust and probably easier to manage if you passed the information to a view. You can get the benefits of an output buffer as well.

If you put your search form into a view and then just pass it the data needed it might get around your problem. Other than that, i can't think of anything to try.
#8

[eluser]sqwk[/eluser]
Thanks, cro_crx, that solved the problem.

Standing in front of a slightly different problem now though:

How do submit form data to a different controller if it validates, but to the same controller if it doesn't?
#9

[eluser]Cro_Crx[/eluser]
You can't really, although there's a redirect() function in the uri helper so you can run that once the validation is true.
#10

[eluser]Unknown[/eluser]
Cro_Crx is right. You should use redirect() function. I use it for my login form.
Code:
if ($admin_type == 'admin_a') {
    redirect('controller_a');
}
else if ($admin_type == 'admin_b') {
    redirect('controller_b');
    }
//you can even redirect to a function
else if ($admin_type == 'admin_c') {
    redirect('controller_c/function_c');
}
//or just want to load a view file
else {
    $this->load->view('login');
}

I hope it can help.




Theme © iAndrew 2016 - Forum software by © MyBB