Welcome Guest, Not a member yet? Register   Sign In
Validation rules from both config and controller
#1

[eluser]crikey[/eluser]
Hi all,

I'm wondering if anyone has ever used a combination of loading form validation rules from a config file and then adding new rules later in the controller.

I want to load some rules from a file to keep them out of the controller (there's lots of fields in my form) and then conditionally add new rules depending on the POST input - if certain fields have data then I want to validate it, but they're not required fields.

As far as I can tell from the user guide, rules from a config file are only processed when the $this->form_validation->run() method is called, and by then it's too late to add other rules.

Does anyone know a way around this that will allow me to do what I want?

Thanks.
#2

[eluser]sketchynix[/eluser]
I'm not sure I understand your reason for doing this. Perhaps I misread it but from what I gather.. "if certain fields have data then I want to validate it, but they’re not required fields".. Why can't you just exclude the 'required' rule in your array for that field?

ie if username were required and age wasn't...
Code:
array(
             'field'   => 'username',
             'label'   => 'Username',
             'rules'   => 'required'
             ),
      array(
            'field'   => 'age',
            'label'   => 'Age',
            'rules'   => 'numeric'
           ),
If that were in the config and the user didnt input their age, the form would still pass validation.
#3

[eluser]freeman[/eluser]
Hi, maybe this should help.

Quote:if certain fields have data then I want to validate it, but they’re not required fields

maybe something like

Code:
if($this->input->post('email'))
{
    // not required but checks users table if email already exists
    $this->set_rules('email', 'Mail', 'valid_email|unique[users.email]');
}

Or a combination of rules from config file as well as rules in your controller

Code:
// in your "test" controller

public function add()
{
    $this->load->library('form_validation');
    $this->load->library('my_form_validation');

    $this->form_validation->set_rules('name', 'Name', 'trim|required');

    if($this->input->post('submit'))
    {
        if($this->form_validation->run() == FALSE)
        {
            // gotcha
            $errors = validation_errors();
        }
    }
}

Code:
// my_form_validation.php

private function prep_config_rules($uri = '')
{
    if($uri === '')
    {
        $uri = trim($this->CI->uri->ruri_string(), '/');
    }

    $this->CI->config->load('form_validation', FALSE, TRUE);
    $rules = $this->CI->config->item($uri);

    if(!empty($rules))
    {
        $this->set_rules($rules);
    }
}

public function run($group = '')
{
    $uri = ($group === '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
    $this->prep_config_rules($uri);
    return parent::run($uri);
}

Code:
// app/config/form_validation.php

$config = array(
    'test/add' => array(
        array(
            'field' => 'username',
            'label' => 'Username',
            'rules' => 'required'
        )
    )
);




Theme © iAndrew 2016 - Forum software by © MyBB