Welcome Guest, Not a member yet? Register   Sign In
codeigniter form validation callback function with multiple arguments issue
#1

[eluser]dottedquad[/eluser]
Hello all,
I am attempting to pass another argument along with with the form element to the set_rules

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

call back function:
Code:
function alias_exist_check($str, $val)
    {
        
        //echo 'value ' . $str;
        list($table, $column) = explode('.', $val, 2);    
          $query = $this->db->query("SELECT COUNT(*) AS count FROM $table WHERE $column = $str'");
          $row = $query->row();
        return ($row->count > 0) ? FALSE : TRUE;
          
    }

error:
A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 1

Filename: controllers/validate_livestock.php

Line Number: 105

line 105:
Code:
list($table, $column) = explode('.', $val, 2);



I slapped that code together after searching google for examples. I am new to the explode code, but do understand the purpose which is, split livestock.alias to variables. However, I am receiving the error explained above. I do not understand what the error means or how to fix this. Any help is greatly appreciated.

-Thank You,
Rich
#2

[eluser]vikascoollives[/eluser]
According to me syntax is wrong try this :

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

[eluser]vikascoollives[/eluser]
Accordingly change your callback function. Second parameter is not all required in callback function in your case .
#4

[eluser]dottedquad[/eluser]
[quote author="vikascoollives" date="1299929925"]According to me syntax is wrong try this :

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

I tried your code and still receiving that error. Also, I had to fix the quotes in your code syntax, but still having no luck.

-Rich
#5

[eluser]vikascoollives[/eluser]
check your callback function now second parameter is not required.
#6

[eluser]dottedquad[/eluser]
The validation is always returning true so, I am attempting to echo out $table and $column for testing purposes. The values are not echoing out so I do not know if the code is working as expected. How do I echo out $table and $column? Thank You.

-Rich

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

        if ($this->form_validation->run() == FALSE)
        {
            //$this->load->view('false'); commented out for testing purposes
        }
        else
        {
            //$this->load->view('true'); commented out for testing purposes
        }

    function alias_exist_check($str)
    {
        
        list($table, $column) = explode('.', $str, 2);    
          $query = $this->db->query("SELECT COUNT(*) AS count FROM $table WHERE $column = $str'");
          $row = $query->row();
        //return ($row->count > 0) ? FALSE : TRUE;
          echo $table . ' ' . $column;
    }
#7

[eluser]skunkbad[/eluser]
Go back to basics:

Code:
// check if first_item and second_item are equal
$this->form_validation->set_rules('first_item', 'FIRST ITEM', 'trim|xss_clean|callback_some_cb_function[' . $this->input->post('second_item') . ']');

if ($this->form_validation->run() == FALSE)
{
    // bad news
    echo 'FAIL';
}
else
{
    // yippee!
    echo 'GOOD';
}

function some_cb_function($str, $str2)
{
    
    // if first_item and second_item are equal
    if($str === $str2)
    {
        // return success
        return $str;
    }
    else
    {
        // set error message
        $this->form_validation->set_message('some_cb_function', 'No match');

        // return fail
        return FALSE;
    }
}
#8

[eluser]davidbehler[/eluser]
Add this code before you run the form validation:
Code:
$this->form_validation->set_rules('alias', 'Alias_Exist', 'trim|xss_clean|callback_alias_exist_check[livestock.alias]');
and add this function to your controller:
Code:
function alias_exist_check($value, $str)
{
   var_dump($value);
   var_dump($str);
   exit();
}

What does the output look like after you submit the form?
#9

[eluser]skunkbad[/eluser]
I'm confused. What is livestock.alias? There is no dollar sign in front of it. Is it defined somewhere?
#10

[eluser]davidbehler[/eluser]
It's a string, nothing else, that's passed as an additional parameter for the callback call. Just like you have additional parameters for other rules, e.g. min_length[5], you can have an additional parameter for callbacks.

In his case he passes a string which contains the table and the field he wants to check.

I don't think it's documented in the user guide, but it definetly works.




Theme © iAndrew 2016 - Forum software by © MyBB