dottedquad
dottedquad

Reputation: 1391

codeigniter form validation callback function with multiple arguments issue: Undefined offset: 1

I am receiving multiple errors which are listed below my code. There was also a mysql error, but I understood that error due to the parts array null values. I have no idea why I am receiving those errors. Can someone explain with a fix also.

line 106: $this->db->where($parts[1], $value);

code:

$this->form_validation->set_rules('alias','alias_exist','trim|xss_clean|callback_alias_exist_check[livestock.alias]');  

    function alias_exist_check($value, $str)
    {

        $parts = explode('.', $str);    
        $this->db->from($parts[0]);
        $this->db->where($parts[1], $value);
        $result = $this->db->get();

        echo $this->db->last_query();
        //return ($row->count > 0) ? FALSE : TRUE;
        //echo $table . ' ' . $column;
    }

error:

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 1

Filename: controllers/validate_livestock.php

Line Number: 106

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/dickschi/public_html/lsms/system/libraries/Exceptions.php:166)

Filename: codeigniter/Common.php

Line Number: 356

Thank You,
Rich

Upvotes: 0

Views: 2190

Answers (2)

danneth
danneth

Reputation: 2791

Why do you need to pass the column name and table to the function? Cant you just do:

function alias_exist_check($value, $str)
{
    $this->db->from('livestock');
    $this->db->where('alias', $value);
    $result = $this->db->get();

    echo $this->db->last_query();
    //return ($row->count > 0) ? FALSE : TRUE;
    //echo $table . ' ' . $column;
}

Upvotes: 1

Réjôme
Réjôme

Reputation: 1484

Did you try to apply this patch ?

https://bitbucket.org/ellislab/codeigniter/issue/139/validation-callback-parameter-does-not

This is a patch for the file

/system/libraries/Form_validation.php

which contains the class CI_Form_validation. In the method _execute() line 491, replace the line

if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))

by

if (preg_match("/(callback_[\[\]\w]+)/", implode(' ', $rules), $match))

Let me know if that doesn't work.

Upvotes: 1

Related Questions