0

I am trying to put validation on a variable value using set_rules function from codeigniter. is it possible to do that?

My code is below :

$client_name = "Some name";
$this->form_validation->set_rules($client_name,'Client','required');

Even i have tried setting the value directly in Post variable as below :

$a = array('Name1','','Name3');

foreach($a as $client_name ) {

    $_POST['client_name'] = $client_name = "Some name";

    echo "--- " . validation_errors();
    $this->form_validation->set_rules('client_name','Client','required');

    if (!$this->form_validation->run())
    {
        echo validation_errors();
    }
    else
    {
        echo 'validation successful';
    }
}

This code is giving below output:

---  // No validation error
validation successful //first time validation successfully ran
---  // No validation error
Client field is required //first time validation failed 
--Client field is required // validation error from the second loop
Client field is required

Please help if it's possible.

Thanks in advance.

1 Answers1

0

I have found following solution:

$a = array('Name1','','Name3');

foreach($a as $client_name ) {
    $this->load->library('form_validation',null,'fv');
    $_POST['client_name'] = $client_name = "Some name";

    echo "--- " . validation_errors();
    $this->fv->set_rules('client_name','Client','required');

    if (!$this->fv->run())
    {
        echo validation_errors();
    }
    else
    {
        echo 'validation successful';
    }
    unset($this->fv);
}

As of now its working but if anyone found better solution please answer this question. because i don't feel its a good practice to load library in loop.