Codeigniter Form Validation...form re-population

10,006

Solution 1

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

Solution 2

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.

Solution 3

   $insertData=array(
            'name'=>NULL,
            'email'=>NULL);
 //setting validation rules
    $this->form_validation->set_rules('name', 'Name', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required|email');

//here you will get all data which user entered in form 
$insertData = $this->input->post(NULL, TRUE);

if ($this->form_validation->run() == FALSE) 
{
    //failed - reload page and re-populate fields with any error messages
    $this->load->view('view_name',$insertData)    

}
else
{
     //set flash data for displaying success message in redirected page
     $this->session->set_flashdata('success','Success message')
     redirect('controller_name');

}

view page

<?php if ($this->session->flashdata('succes_message')) { echo    $this->session->flashdata('succes_message'); ?> } ?>

<form action='action page url' method='post'>
<input type='text' name='name' value='<?php echo $name ?>'/>
<input type='text' name='email' value='<?php echo $email ?>'/>
<input type='submit' value='Save'/>
Share:
10,006
Michael Bradley
Author by

Michael Bradley

Updated on June 08, 2022

Comments

  • Michael Bradley
    Michael Bradley almost 2 years

    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

  • Michael Bradley
    Michael Bradley almost 15 years
    Umm, that's what I thought. It's still a pretty round about way, but probably the simplest solution nonetheless. It would be handy if an array could be passed through a redirect...thanks tj111
  • user1260501
    user1260501 almost 15 years
    The problem with this is that client-side validation is easily by-passable. You should always validate data on the server, even if you pre-validate it on the client. Never trust data you don't control.
  • GloryFish
    GloryFish almost 15 years
    Flashdata is a great suggestion. Easy to use, clean code. codeigniter.com/user_guide/libraries/sessions.html
  • stef
    stef over 14 years
    I think this is actually the most CI way of doing it but for some reason everyone, including me, always ends up using session methods.
  • Randell
    Randell about 14 years
    IMO, using the session methods end up with clutter in the code.