Welcome Guest, Not a member yet? Register   Sign In
Reset Form Validation Rules / Validation Groups Not In Configuration File
#1

[eluser]oribani[/eluser]
Greetings,

I appreciate the effort of the CI developers to allow for the abstraction of form validation into configuration files, but I don't agree with that model. It's mostly down to your own design style, but for me, I think that the rules you use to validate incoming data should be something you can see when reading controller code. I think configuration files should hold settings that do not define logic (at the most, these settings can turn logic switches on and off, but that should be as far as it goes). Validation rules are logic, and therefore, to me, do not belong in configuration files.

So no problem, CI lets you define the rules you run in your controller. The shortcoming is that CI does not support the concept of grouped rules unless you define them in a configuration file. Bummer. This is especially problematic when processing RPC requests and you want to run one rule at a time so you can send back a validation error when you hit one. What you need is either a way to define and execute a dynamic validation rule group in your controller or a way to reset the rules stored in the validator after running each check. I dug into the Form Validator class and found a hack to make the latter idea work.

Code:
$this->form_validation->_error_array = array();
$this->form_validation->_field_data = array();

Here is how I run a rule on two POST variables one at a time:

Code:
$this->form_validation->set_rules('employee_id', 'employee ID', 'trim|required|is_natural_no_zero');
if (!$this->form_validation->run())
{
   [call custom error routine here then exit or whatever]
}

// reset validation rules (would be much
// better if there were an API call for this)
//
$this->form_validation->_error_array = array();
$this->form_validation->_field_data = array();

$this->form_validation->set_rules('first_name', 'first name', 'trim|required|xss_clean');
if (!$this->form_validation->run())
{
   [call custom error routine here then exit or whatever]
}

You could use custom validation callbacks to check all your incoming data within which you can of course send back an RPC response and exit if desired, but that's questionable from a design perspective and more work (the validation works fine the way it is!).




Theme © iAndrew 2016 - Forum software by © MyBB