Codeigniter Form Validation...form re-population


I am in the process of creating an input form on a record specific page e.g. (property/rentals/rec_id). On submission, the data should be inserted, validated and return to the original page from which it was sent with any relevant messages.

  • If the validation fails, reload the form page a re-populate the form
  • Echo the specific validation error - e.g. invalid email address etc

  • If successful, insert data, reload the page and display success message.

    function class() {

    $fields['name'] = 'Name';
    $fields['email'] = 'Email Address';
    if ($this->form_validation->run() == FALSE) 
        //failed - reload page and re-populate fields with any error messages
         // display form with success message


Normally this is simply done by 'loading a view' and passing the relevant data to repopulate the page, fields or display messages. But I have to use a 'redirect' back to the product specific page. Is there any good way to do this other than using session data to carry the validation errors..?

Thanks in advance, Michael

7/13/2009 5:27:33 PM

Accepted Answer

Take a look at the CodeIgniter manual on Sessions and see if 'FlashData' is of any use.

7/13/2009 6:04:49 PM

You may use the set_value('field_name') function to re-populate the form. Sample usage in view:

<input type="text name="name" value="<?php echo set_value('name'); ?>" />"

To display the errors from the form_validation, you may use the validation_errors() function. Sample usage in view:

<?php echo validation_errors(); ?>

Also, see link text for more info.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow