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]
  • #16 / Mar 14, 2011 2:53am

    dottedquad

    27 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;
            }
        }
    }

    Thank You skunkbad.  Of course your code works as expected.  Everyone else code most likely works also.  My code executes via AJAX instead of post-back(terminology?)  I will have to break apart your code and plug the pieces in.

    -Thank You,
        Rich

  • #17 / Mar 14, 2011 1:29pm

    dottedquad

    27 posts

    After debugging your example I noticed a strange behaviour.  The code worked as expected, but as soon as I removed the required from the set_rules I would receive the errors I mentioned before.  I am checking form validation via AJAX.  Jquery executes an AJAX POST function on change.  I am not doing the ‘traditional’ form validation using a submit button.  I am officially stumped.  Maybe a patch is required?  Would it help if I uploaded the javascript, form, and codeigiter code?

    -Thank You,
        Rich

  • #18 / Mar 14, 2011 3:44pm

    skunkbad

    1326 posts

    After debugging your example I noticed a strange behaviour.  The code worked as expected, but as soon as I removed the required from the set_rules I would receive the errors I mentioned before.  I am checking form validation via AJAX.  Jquery executes an AJAX POST function on change.  I am not doing the ‘traditional’ form validation using a submit button.  I am officially stumped.  Maybe a patch is required?  Would it help if I uploaded the javascript, form, and codeigiter code?

    -Thank You,
        Rich

    Yes, of course it would help to see what you are dealing with. The problem for me is yesterday was my day and I had more time to help. I’m sure somebody else will help though.

  • #19 / Mar 14, 2011 11:20pm

    dottedquad

    27 posts

    I just figured it out.  My issue was with the Data in my AJAX function and not CI.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases