0

I have a from which lists several items that can be selected by check box and a dropdown permitter added. In a separate part of the form is have another check box. This is submitted as two arrays.

The arrays look like this

Array
(
[1] => Array
    (
        [num_copy] => 1
        [dwg_rev] => B
        [dwg_id] => 1
    )

[2] => Array
    (
        [num_copy] => 1
        [dwg_rev] => B
        [dwg_id] => 2
    )

)
Array
(
[1] => Array
    (
        [client_id] => 1
    )

)

I need to pass these two arrays to a form_validation that can check is dwg_rev has already been added to the database for the selected client_id.

I tried to use is_unique, but it can only check in one table to see if this already exists in it.

This is what my form validation currently looks like

$rows = array();

    if(!empty($_POST['result']))
    {
        $rows = $_POST['result'];
        $temp_dwg_array = array_column($rows, 'temp_dwg_id');
        foreach($temp_dwg_array as $key => $temp_dwg_id)
        {
            $this->form_validation->set_rules('result['.$temp_dwg_id.'][dwg_rev]', 'Revision' , 'required|callback_check_issued_rev');
        }
    } else $this->form_validation->set_rules('result[][temp_dwg_rev]', 'Revision' , 'required');

    if(!empty($_POST['client']))
    {
        $temp_client_array = array_column($_POST['client'],'client_id');
        foreach($temp_client_array as $key => $client_id)
        {
            $this->form_validation->set_rules('client['.$client_id.'][client_id]', 'Please make a selection from the distribution list' , 'required|callback_check_issued_rev');
        }
    }
    else $this->form_validation->set_rules('client[][client_id]', 'Distribution' , 'required');

I want to create a callback function, but I can't figure out how to pass two variables to the callback function to compare to the db.

Juan-Emil Saayman
  • 198
  • 1
  • 3
  • 18
  • Couldn't you use `is_unique` as few times rule for different tables `is_unique[table1.col4]|is_unique[table2.col3]`? – Tpojka Apr 02 '16 at 09:17
  • I think I'm going to need to pass the two arrays to a callback function and then check if I can find the values in the db tables with a join and then return true or false... the passing of the post arrays to the callback function what I'm struggling with. – Juan-Emil Saayman Apr 02 '16 at 15:30
  • Do you want to check two values in one table or two values in two tables? – Tpojka Apr 02 '16 at 15:42
  • I want to check two values in two tables. – Juan-Emil Saayman Apr 02 '16 at 16:02
  • Than you can use proposed solution for both values separately. – Tpojka Apr 02 '16 at 21:05
  • I'm wondering if there isn't a way pass the post array to the callback function like you would normally and assign it to a variable. Something like this answer in this post with a `$this->input>post()` [Codeigniter passing 2 arguments to callback](http://stackoverflow.com/questions/4822692/codeigniter-passing-2-arguments-to-callback) – Juan-Emil Saayman Apr 04 '16 at 11:11
  • You can always see what [docs](https://codeigniter.com/userguide3/libraries/form_validation.html#callbacks-your-own-validation-methods) (If you need to receive an extra parameter in your callback method, just add it normally after the method name between square brackets, as in: “callback_foo[bar]”, then it will be passed as the second argument of your callback method.) propose. But in that case you need to write more extra code like callback and specific model methods. I offered you already made solution. – Tpojka Apr 04 '16 at 11:56
  • @Tpojka - I found a solution to what I wanted to do here http://www.robertmullaney.com/2010/11/04/codeigniter-form-validation-callback-functions-with-multiple-arguments/. I can now pass the parameters to the callback function, but if i select value that exists in the db and one that doesn't pick up that there is already are record in the db. i.e `[1] => Array ( [num_copy] => 1 [dwg_rev] => B [dwg_id] => 1 ) ` is already in the db, but `[2] => Array ( [num_copy] => 1 [dwg_rev] => B [dwg_id] => 2 )` is not. – Juan-Emil Saayman Apr 08 '16 at 10:30
  • Check [this q/a](http://stackoverflow.com/questions/19324420/callback-function-in-codeigniter-with-multiple-parameters-during-form-validation). – Tpojka Apr 08 '16 at 10:40

0 Answers0