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

[eluser]skunkbad[/eluser]
[quote author="waldmeister" date="1300045536"]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.[/quote]

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

[eluser]davidbehler[/eluser]
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:
Code:
$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:
Code:
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

[eluser]skunkbad[/eluser]
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

[eluser]dottedquad[/eluser]
[quote author="waldmeister" date="1300046957"]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:
Code:
$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:
Code:
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.[/quote]

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:
Code:
this->db->where($parts[1], $value);

set_rules:

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

Call back function:

Quote: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

[eluser]skunkbad[/eluser]
This is a working example, which more or less confirms that your code should work:

Code:
<?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;
        }
    }
}
#16

[eluser]dottedquad[/eluser]
[quote author="skunkbad" date="1300082695"]This is a working example, which more or less confirms that your code should work:

Code:
<?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;
        }
    }
}
[/quote]

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

[eluser]dottedquad[/eluser]
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

[eluser]skunkbad[/eluser]
[quote author="dottedquad" date="1300138147"]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[/quote]

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

[eluser]dottedquad[/eluser]
I just figured it out. My issue was with the Data in my AJAX function and not CI.




Theme © iAndrew 2016 - Forum software by © MyBB