TCM
TCM

Reputation: 16900

How do I preserve form values?

I have a controller which loads the view like this:

    class A extends Controller{
        public function simpleForm(){
          //this generates the form
        }

        public function simpleFormSubmitted(){
          // Simple form is submitted here. Here I perform validation and if
          // validation fails I want to display simpleform again with all the values
          // inputted by the user as it is and if validation succeeds I want to
          // redirect to some other page. I am using simple HTML to generate the
          // form and not the formhelper of CodeIgniter because I am more comfortable
          // with HTML rather than remembering the syntax of CodeIgniter.
        }
    }

Upvotes: 0

Views: 448

Answers (2)

vikmalhotra
vikmalhotra

Reputation: 10071

Here is how to preserve the form fields when dealing with errors...

$fields['username'] = 'Username';
$fields['password'] = 'Password';
$fields['passconf'] = 'Password Confirmation';
$fields['email'] = 'Email Address';

$this->validation->set_fields($fields);

This is how to re-populate the HTML form...

<?php echo $this->validation->error_string; ?>

<?php echo form_open('form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="<?php echo $this->validation->username;?>" size="50" />

For more information look over here - Form Validation in Codeigniter Look for the heading Re-populating the form

Upvotes: 2

m1k3y3
m1k3y3

Reputation: 2788

in function simpleForm add default variables so, after validation you can call it again with userform values

class A{
        public function simpleForm($val1="", val2="", $val3=""){
          //this generates the form
        }

        public function simpleFormSubmitted(){
          //simple form is submitted here. Here is perform validation and if validation   fails i 
      if(!$valid){

          $result = $this->simpleForm($val1, $val2, $val3 etc...)
       }
      else{
      $result = //whatever you need to output after validation success
      }
      return $result 
      }
    }

Upvotes: 0

Related Questions