ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

codeigniter form validation callback function with multiple arguments issue

March 11, 2011 11:46pm

Subscribe [3]
  • #1 / Mar 11, 2011 11:46pm

    dottedquad

    27 posts

    Hello all,
      I am attempting to pass another argument along with with the form element to the set_rules

    set_rules code:

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

    call back function:

    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:

    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 / Mar 12, 2011 1:38am

    vikascoollives

    25 posts

    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 / Mar 12, 2011 1:40am

    vikascoollives

    25 posts

    Accordingly change your callback function. Second parameter is not all required in callback function in your case .

  • #4 / Mar 12, 2011 1:44am

    dottedquad

    27 posts

    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]’);

    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 / Mar 12, 2011 1:51am

    vikascoollives

    25 posts

    check your callback function now second parameter is not required.

  • #6 / Mar 13, 2011 12:45am

    dottedquad

    27 posts

    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:

    $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 / Mar 13, 2011 5:26am

    skunkbad

    1326 posts

    Go back to basics:

    // 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 / Mar 13, 2011 9:15am

    davidbehler

    708 posts

    Add this code before you run the form validation:

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

    and add this function to your controller:

    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 / Mar 13, 2011 11:39am

    skunkbad

    1326 posts

    I’m confused. What is livestock.alias? There is no dollar sign in front of it. Is it defined somewhere?

  • #10 / Mar 13, 2011 11:45am

    davidbehler

    708 posts

    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.

  • #11 / Mar 13, 2011 11:52am

    skunkbad

    1326 posts

    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.

    I thought a string had to be quoted, so that was my confusion. Also, if the string is basically hard coded as an argument to the callback function, then there’s really no point in having it there. Maybe he/she is doing it that way for testing purposes.

  • #12 / Mar 13, 2011 12:09pm

    davidbehler

    708 posts

    Why is there no point?
    It’s not hardcoded into the callback function but rather into the list of rules that are being applied to a certain field.

    You can still use a different string for a different field:

    $this->form_validation->set_rules('alias', 'Alias_Exist', 'trim|xss_clean|callback_alias_exist_check[livestock.alias]');  
    $this->form_validation->set_rules('username', 'Username_Exist', 'trim|xss_clean|callback_alias_exist_check[user.name]');  
    $this->form_validation->set_rules('foobar', 'Foobar_Exist', 'trim|xss_clean|callback_alias_exist_check[foo.bar]');

    and if your callback function looks like this, then 3 different queries will be run:

    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();
    
       if($result->num_rows() > 0) {
          $this->form_validation->set_message('alias_exist_check', 'Already exists.');
          return FALSE;
       } else {
          return TRUE;
       }
    }

    Or even if you don’t have multiple fields in one form that you need to run this check on, you could move the callback function into your own controller class (MY_Controller) and use it in multiple controllers for different fields.

    Btw: I would move the db related stuff into the model, but that’s up to you.

  • #13 / Mar 13, 2011 12:16pm

    skunkbad

    1326 posts

    Yes, I see your point, and I think it’s actually a nice idea for some similar functionality in callback functions of my own. I was initially just confused because of the lack of quotes.

  • #14 / Mar 13, 2011 6:47pm

    dottedquad

    27 posts

    Why is there no point?
    It’s not hardcoded into the callback function but rather into the list of rules that are being applied to a certain field.

    You can still use a different string for a different field:

    $this->form_validation->set_rules('alias', 'Alias_Exist', 'trim|xss_clean|callback_alias_exist_check[livestock.alias]');  
    $this->form_validation->set_rules('username', 'Username_Exist', 'trim|xss_clean|callback_alias_exist_check[user.name]');  
    $this->form_validation->set_rules('foobar', 'Foobar_Exist', 'trim|xss_clean|callback_alias_exist_check[foo.bar]');

    and if your callback function looks like this, then 3 different queries will be run:

    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();
    
       if($result->num_rows() > 0) {
          $this->form_validation->set_message('alias_exist_check', 'Already exists.');
          return FALSE;
       } else {
          return TRUE;
       }
    }

    Or even if you don’t have multiple fields in one form that you need to run this check on, you could move the callback function into your own controller class (MY_Controller) and use it in multiple controllers for different fields.

    Btw: I would move the db related stuff into the model, but that’s up to you.

    As soon as I get the field value to validate with the database I will definitely move it to a model.

    All the help and code presented to me does make sense, but the error message I am receiving does not.

    line 106 is:

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

    set_rules:

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

    Call back function:

    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

  • #15 / Mar 13, 2011 10:04pm

    skunkbad

    1326 posts

    This is a working example, which more or less confirms that your code should work:

    <?php
    
    // Test DB
    
    /********************************************
    
    CREATE TABLE IF NOT EXISTS `some_table` (
      `id` int(11) NOT NULL,
      `some_value` int(11) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    
    INSERT INTO `some_table` (`id`, `some_value`) VALUES
    (1, 10),
    (2, 20);
    
    *********************************************/
    
    class Test_validation extends CI_Controller {
    
        public function index()
        {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('field_1', 'Field One', 'required|callback__field_one_checker[some_table.id]');
    
            if ($this->form_validation->run() == FALSE)
            {
                echo 'either no post submitted, or validation failed';
    
                if(validation_errors())
                {
                    echo validation_errors();
                }
            }
            else
            {
                echo 'success';
            }
    
            echo '<form action="#" method="post">
                    <input type="text" name="field_1" value="" />
                    <input type="submit" value="submit" />
                </form>';
        }
    
        function _field_one_checker($field_one, $table_column)
        {
            /* THIS ALSO WORKS
    
            list($table_name, $column) = explode('.', $table_column);
            if($query = $this->db
                        ->where($column, $field_one)
                        ->limit(1)
                        ->get($table_name)
            )
    
            */
    
            $parts = explode('.', $table_column);
            if($query = $this->db
                        ->where($parts[1], $field_one)
                        ->limit(1)
                        ->get($parts[0])
            )
            {
                if($query->num_rows() > 0)
                {
                    return $field_one;
                }
                else
                {
                    $this->form_validation->set_message('_field_one_checker', 'Your value was not an ID in some_table');
                    return FALSE;
                }
            }
            else
            {
                $this->form_validation->set_message('_field_one_checker', 'Query failed');
                return FALSE;
            }
        }
    }
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases