Welcome Guest, Not a member yet? Register   Sign In
Multiple Fields with a single callback using the form_validation library?
#7

[eluser]MadZad[/eluser]
drewbee's posts got me thinking and experimenting, and I came across something unexpected and convenient for producing error messages when dealing with multiple fields in a custom validation function. distilled down, here's my setup:
Code:
$this->rules['bigfield'] = 'required';
$this->fields['bigfield'] = 'Big number';
$this->rules['smallfield'] = 'required|callback__not_more_than[bigfield]';
$this->fields['smallfield'] = 'Small number';

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

here's my callback function:
Code:
function _not_more_than($val, $check_field) {
    if ($val <= $_POST[$check_field]) return true;

    $this->validation->set_message('_not_more_than', "Error: '%s' is more than '%s'");
    return false;
}

From the docs, I did expect the first "%s" to turn into "Small number", but the second "%s" turned into "Big number". CI actually reaches into the fields array I fed set_fields to turn the callback's second parameter into the actual field name. Nice. That makes the error message:

Quote:Error: 'Small number' is more than 'Big number'

Disclaimer: $_POST has already been whitelisted and validated. Using the raw $_POST is verboten.


Messages In This Thread
Multiple Fields with a single callback using the form_validation library? - by El Forum - 05-29-2009, 12:50 PM



Theme © iAndrew 2016 - Forum software by © MyBB