Welcome Guest, Not a member yet? Register   Sign In
Reset of Validation possible?
#1

[eluser]esbium[/eluser]
Hey everyone,

I have a form that submits to a controller, gets validated and then inserts the data into the database. All of that works fine. However, once all of that is done, I want to re-display the same form so the user can enter another record. When I do this, the old values from the last submit remain in the form. I do not want to use a redirect here.

I have tried creating a new instance of CI_Validation after a successful submit in hopes that it would reset all the values that were stored in it but that doesn't work.

Basically, the calls to $validation->someFormField return the last value from the submit. I know that this is great when there are errors, but I need to clear them if there are no errors!

Please help and thanks in advance!
#2

[eluser]Luci3n[/eluser]
try sub classing the validation class

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

   class MY_Validation extends CI_Validation {

       function My_Validation()
       {
           parent::CI_Validation();
       }

       function set_default_values($data, $value = null)
       {
           if (is_array($data) == TRUE)
           {
               foreach($data as $field => $value)
               {
                   $this->$field   = $value;
                   $_POST[$field]  = $value;
               }
           }
           else
           {
               $this->$data    = $value;
               $_POST[$data]   = $value;
           }
       }
   }
?>

for further info see this post
#3

[eluser]Optimus Prime[/eluser]
Hi [esbium],

You can try to unset the validation:

Code:
if ($this->validation->run() == TRUE) {
   //... All you code here...

   //... At the end unset validation
   unset($this->validation)
}

This will unset the validation object, but only when validation->run() is true. In this way you can use the validation->error_string in case that validation is false.

But if you need to display default values, try the Lunic3n's method.

I hope this help you.




Theme © iAndrew 2016 - Forum software by © MyBB