Welcome Guest, Not a member yet? Register   Sign In
Patch for Validation class - retain preloaded data
#1

[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)
// WARNING: this code is illustrative, it does not work with CI 1.5.4!!!

// set up form with data stored in session
$sessiondata = $this->session->userdata('form');
if ( ! is_array($sessiondata) )
    $sessiondata = array();
foreach ( $sessiondata as $key -> $value ) {
    $this->validation->$key = $sessiondata[$key];
}

// overwrite with new data from $_POST
$this->validation->set_fields($fields);

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]) )
{
    $this->$key = $this->prep_for_form($_POST[$key]);
}

if ( ! isset($this->$key) )
{
    $this->$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 (count($_POST) == 0 OR count($this->_rules) == 0)
// to:
if (count($this->_rules) == 0)

// line 200, change:
if ( ! isset($_POST[$field]) OR $_POST[$field] == '')
// to:
if ( ! isset($this->$field) OR $this->$field == '')

// line 215, change:
if ( ! isset($_POST[$field]))
// to:
if ( ! isset($this->$field))

// line 271, since we could be calling a callback function from any controller handling
// the form I've added provision to use a helper. Change:
if ( ! method_exists($this->CI, $rule))
// to:
if ( ! method_exists($this->CI, $rule) && ! function_exists($rule) )
//line 276, change:
$result = $this->CI->$rule($_POST[$field], $param);
// to:
if ( method_exists($this->CI, $rule) )
{
    $result = $this->CI->$rule($this->$field, $param);      
}
if ( function_exists($rule) )
{
    $result = $rule($this->$field, $param);
}

// line 298, using a native PHP function, change:
$_POST[$field] = $rule($_POST[$field]);
$this->$field = $_POST[$field];
// to:
$this->$field = $rule($this->$field);

// line 305, using a method of the validation class, change:
$result = $this->$rule($_POST[$field], $param);
// to:
$result = $this->$rule($this->$field, $param);

// line 403, 'match' rule, change:
if ( ! isset($_POST[$field]))
// to:
if ( ! isset($this->$field))
// and change:
return ($str !== $_POST[$field]) ? FALSE : TRUE;
// to:
return ($str !== $this->$field) ? FALSE : TRUE;

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


Messages In This Thread
Patch for Validation class - retain preloaded data - by El Forum - 09-30-2007, 05:44 PM
Patch for Validation class - retain preloaded data - by El Forum - 10-01-2007, 01:35 PM
Patch for Validation class - retain preloaded data - by El Forum - 10-01-2007, 02:36 PM
Patch for Validation class - retain preloaded data - by El Forum - 10-03-2007, 02:26 PM



Theme © iAndrew 2016 - Forum software by © MyBB