Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is that you're using two separate functions to do form handling. The form validation class docs don't really explain it well, and it took me awhile to realize it but the form_validation->run() returns false if there is an error, but also if it is a GET request, and subsequently accounts for the GET request in the related functions like the form_error(), and validation_errors(), set_value(), etc.</p> <p>The best practice in CI (and in general) is to do this:</p> <pre><code>class Welcome extends CI_Controller{ function home(){ $this-&gt;load-&gt;view('home'); } function form() { // make the 'quality' field required $this-&gt;form_validation-&gt;set_rules('quality', 'Quality', 'required'); // If the fields are NOT filled in... // or if there isn't a POST! (check the Form_validation.php lib to confirm) if ( $this-&gt;form_validation-&gt;run() === FALSE) { // This form_error() function actually doesn't do anything if there // wasn't a form submission (on a GET request) echo form_error('quality'); $this-&gt;load-&gt;view('form'); // load or reload the page } else // if the fields are filled in... { // set success message in flashdata so it can be // called when page is redirected. $this-&gt;session-&gt;set_flashdata('message', 'Your rating has been saved'); redirect('welcome/home','location', 303); exit; } } </code></pre> <p>then in the view have the form <code>action="welcome/form"</code> </p> <p>Basically all of the form error functions and all the stuff related to form validation have checks to see if the form validator actually ran... here is an example from the form_error function in the form helper file</p> <pre><code>function form_error($field = '', $prefix = '', $suffix = '') { if (FALSE === ($OBJ =&amp; _get_validation_object())) { return ''; } return $OBJ-&gt;error($field, $prefix, $suffix); } </code></pre> <p>When their isn't a POST, it shows as normal, and has the natural page flow you are looking for. </p> <p>Unrelated to the question, but confusing/noteworthy about the form validation class... if you use the filters like xss_clean, prep_url, etc. in the parameters field, it actually repopulates the $_POST array for you, so you don't really need to do anything extra.</p> <p>Sometimes it's worth taking a look at the internals of the CI source, there's some clever stuff in there which isn't entirely obvious.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload