Welcome Guest, Not a member yet? Register   Sign In
Variable form validation with separate validation config file
#1

[eluser]rebellion[/eluser]
I have a webapp where I'm using a lot of forms for adding and editing data.

To have a nice overview over all the validation that is going on, I've added all the validation rules in form_validation.php in the application config folder.

But I have some questions regarding variable validation. Based on what the user selects in the form, some input fields are required in one combination, but not in others.

Earlier I had all the validation rules in the controller, and did a check on the post values for which fields should be validated for what, but how would I solve that when I have all the rules in a separate config file?
#2

[eluser]überfuzz[/eluser]
Slightly off topic, but it might help you...

I see no real backside of having the validation set-up in the controllers. And it's very tricky to separate the validation from the controller. There is a way to make the controller tidy, or tidier, even with the validation in the controller. Separate the validation and put it in a function.

Code:
function _validate_form_fields()
{
    $this->form_validation->set_rules('name', 'Name', 'trim|required');
    //Etc...

    return $this->form_validation->run();
}

I normally use it like this.
Code:
if($this->_validation_form_fields())
{
   $this->index();
}

Even if you're putting the form_validation in the controller it doesn't feel bloated.
#3

[eluser]rebellion[/eluser]
Thanks for your reply. It doesn't seem like a bad idea to put the validation in a function. Might just do that! Smile




Theme © iAndrew 2016 - Forum software by © MyBB