CodeIgniter Forums

Full Version: Variable form validation with separate validation config file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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?

El Forum

[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.

El Forum

[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