Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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() {

    $this->load->library('form_validation');    
    $this->form_validation->set_rules('name','name','required|trim');
    $this->form_validation->set_rules('email','email','required|trim|valid_email');
    
    $fields['name'] = 'Name';
    $fields['email'] = 'Email Address';
    
    $this->validation->set_fields($fields);
    
    if ($this->form_validation->run() == FALSE) 
    {
        //failed - reload page and re-populate fields with any error messages
    }
    else
    {
         // 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

like image 463
Michael Bradley Avatar asked Jul 17 '26 23:07

Michael Bradley


2 Answers

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.

like image 136
Randell Avatar answered Jul 20 '26 15:07

Randell


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

like image 42
amr Avatar answered Jul 20 '26 16:07

amr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!