Patch for Validation class - retain preloaded data - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: Patch for Validation class - retain preloaded data (/showthread.php?tid=3431) |
Patch for Validation class - retain preloaded data - El Forum - 09-30-2007 [eluser]Darren Inwood[/eluser] Hi there If you use the set_fields() method of the validation class, the current behaviour is to zero the field if a value was not submitted from the previous page. This means you cannot set default values, and then use this function to overwrite them with the new values, which is especially useful when you have a multi-page form. Eg. Code: // (inside controller) This is due to the following line of code in Validation.php (line 90): Code: $this->$key = ( ! isset($_POST[$key]) OR is_array($_POST[$key])) ? '' : $this->prep_for_form($_POST[$key]); I've replaced this with the following snippet: Code: if ( isset($_POST[$key]) AND ! is_array($_POST[$key]) ) AFAIK this is functionally equivalent, but only blanks the field if there isn't an existing value. NB: Personally I like to escape my data according to the output format I'm using it in, eg using Smarty's {$variable|escape:'format'} commands, so I ommitted the prep_for_form function. Now the class assigns $_POST variables to internal variables, unfortunately it still refers to $_POST for the actual validation. You also need to alter a few lines throughout: Code: // line 183, we still want to test this even if no data was POSTed, so change: If you omit the prep_for_form function, these should also be functionally equivalent to the current class. If you leave it in, I can't see any reason why it wouldn't also work. HTH someone! =) -- Darren Patch for Validation class - retain preloaded data - El Forum - 10-01-2007 [eluser]alpar[/eluser] Isn't setting up predefined options after you set up fields an option? personally i use the $fields array i set the fields from , in the foreach statement to set some default values. It acts as a white list too. Patch for Validation class - retain preloaded data - El Forum - 10-01-2007 [eluser]Darren Inwood[/eluser] I don't understand - how do you use the $fields array to set values? Do you conditionally put the values into $_POST? Anyway, the upshot is, if you alter the Validation class as above, both of us can work how we're used to. Currently, everything I write turns to custard if I use the Validation class as-is, as I'm not used to its' side-effects. I think that this class should at least change it's behaviour to stop coupling itself to the $_POST array. set_fields() copies the $_POST data we're interested in into the internal variables. Continuing to use $_POST throughout the run() and various validation methods couples the class to the $_POST global array unecessarily IMO. Eg: Code: $fields['foo'] = 'Foo'; Patch for Validation class - retain preloaded data - El Forum - 10-03-2007 [eluser]stefanv[/eluser] Real simple to solve.. Create a new class in your library directory in you appliction, with the following code: Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); This will overwrite the set_fields function in the CI_Validation class. Now use something like this in your code to either use data from the database or for the post form: Code: // Call database Works for me ! |