Welcome Guest, Not a member yet? Register   Sign In
Callback when using form_validation.php $config array
#1

[eluser]Ickes[/eluser]
Using form_validation.php to store all my rules in one config file as described in the form validation part of the user guide. With normal rules, the file works perfectly fine. Great.

I can't get a custom callback to work properly. This is my first attempt at a callback so bear with me.

The form has a dropdown menu with 3 options. For the sake of argument, 1 = yes, 2 = no, 3 = maybe. These options are stored in a table with table_id = 1 = yes, table_id = 2 = no, table_id = 3 = maybe.

I want a callback that checks if the user choice is within the range of possible choices. That is, if the user somehow spoofed the form and entered 700 as the choice, I want that to fail.

Here is what I have.
Code:
//Part of form_validation.php
//This part is working fine except when I add the callback part
array(
        'field' => 'choice',
    'label' => 'Dropdown Choice',
    'rules' => 'required|is_natural_no_zero|callback_in_domain["tbl_choices"]'                )

Code:
//farther down in the form_validation.php file
function in_domain($form_value, $table_name)
{
    if(isset($table_name))
    {
        $stmt = $dbh->query("SELECT COUNT(*) FROM $table_name");
        $stmt->execute();
        $count = $stmt->fetchColumn();
        if($form_value <= $count)
        {
            return TRUE;
                        exit();
        }
    }
    $this->form_validation->set_message('choice', 'The %s field can not be the word "test"');
    return FALSE;
}

So lets call all of that part 1.
#2 - Can the function be placed in the form_validation.php file or should it be placed in the controller or somewhere else?
#3 - I believe this function should be private to prevent someone from submitting with their own table name - hack attempt

Thanks.
#2

[eluser]Iván Argulo[/eluser]
I think the best way to do this is having a function (as you say at #3, better if private), something like this:

Code:
array(
    'field' => 'choice',
    'label' => 'Dropdown Choice',
    'rules' => 'required|is_natural_no_zero|callback_in_domain' // NOTE: no params passed                
)

and your function

Code:
function in_domain($form_value) {
    // Do all stuff here
}

Note that you can pass only one param, the value of the current field. So you'll have to write several functions for your tables, or store the name of the table to check in a session, before loading the validation rules, and check for it in the callback.

Hope it helps
#3

[eluser]Ickes[/eluser]
Thanks for the reply Ivan. I'm not sure I asked my question properly.

What do I need to do to get this to work? I tried just as you stated -> removing the parameter and returned a simple FALSE. Still not working - seems the code doesn't ever check this callback function.
Code:
function in_domain($passed_value)
{
        //the code can only return FALSE
    $this->form_validation->set_message('topic', 'The %s field can not be the word "test"');
    return FALSE;
}

So, restated
#1 - can someone help me to get this callback to run?
#2 - is the callback function supposed to be included in the form_registration.php file or within the controller or somewhere else altogether?

Thanks.

Quote:Note that you can pass only one param, the value of the current field. So you’ll have to write several functions for your tables, or store the name of the table to check in a session, before loading the validation rules, and check for it in the callback.

Also, I'm not sure this is accurate. I have seen a tutorial or two where multiple parameters have been passed. Either way, just want it to work simply and then I can go from there.
#4

[eluser]Iván Argulo[/eluser]
Hi

I'm testing your code in my server, so I hope I'll find something.

[quote author="Ickes" date="1247617988"]
#2 - is the callback function supposed to be included in the form_registration.php file or within the controller or somewhere else altogether?
[/quote]

Within the controller is the best.

[quote author="Ickes" date="1247617988"]
Also, I'm not sure this is accurate. I have seen a tutorial or two where multiple parameters have been passed. Either way, just want it to work simply and then I can go from there.[/quote]

I just stated what the User Guide says; I've never used a callback with several parameters, so I don't know if this works... but it will be a nice thing to test Smile
#5

[eluser]Iván Argulo[/eluser]
Maybe it's not the point, but #1 works, #2 doesn't work

#1
Code:
$config = array(
    array(
        'field' => 'choice',
        'label' => 'Dropdown Choice',
        'rules' => 'required|is_natural_no_zero'
    )                
);

#2
Code:
$config = array(
    'field' => 'choice',
    'label' => 'Dropdown Choice',
    'rules' => 'required|is_natural_no_zero'
);
#6

[eluser]Iván Argulo[/eluser]
This worked fine for me...

Code:
function index()
{
    $this->load->helper(array('form', 'url'));
    
    $this->load->library('form_validation');
        
    $config = array(
        array(
            'field' => 'choice',
            'label' => 'Dropdown Choice',
            'rules' => 'required|is_natural_no_zero|callback_in_domain["tbl_choices"]'
        )                
    );
    
    $this->form_validation->set_rules($config);
        
    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('myform');
    }
    else
    {
        $this->load->view('formsuccess');
    }
}

function in_domain($form_value, $table_name)
{
    echo $form_value . ' ' . $table_name;
    
    
    
    if ($form_value == 1)
    {
        $this->form_validation->set_message('choice', 'The %s field can not be the word "test"');
        return false;
    }
    else
    {
        return true;
    }
}
#7

[eluser]Ickes[/eluser]
Thanks for the help Ivan. #1 is closest to what my original file looks like. For the sake of brevity, I did not include the entire $config code with all the arrays used to check each field.

[quote author="Iván Argulo" date="1247619111"]Maybe it's not the point, but #1 works, #2 doesn't work

#1
Code:
$config = array(
    array(
        'field' => 'choice',
        'label' => 'Dropdown Choice',
        'rules' => 'required|is_natural_no_zero'
    )                
);
[/quote]

I have been able to get it to process that part no problem. The problem is when I add a callback using a custom function.

Can anyone help me understand why my custom function will not run when I include it as a rule? If I am not being clear, please let me know and I will try again.

Thanks.
#8

[eluser]Ickes[/eluser]
Thanks a lot Ivan. Really appreciate the effort.
[quote author="Iván Argulo" date="1247619402"]This worked fine for me...[/quote]

Try as I might I just can't get my code to access the callback function. I used exactly what Ivan posted and still no luck. I can access all the predefined rules - no problem.

Anyone have any ideas why the custom callback function will not access? Really weird.

EDIT: Got it!!!

Had one extra } that didn't belong which inadvertantly placed my code outside of my controller. Additionally, to make the custom error message work I changed to....
Code:
$this->form_validation->set_message('in_domain', 'The %s field can not be the word "test"');

Ivan, thanks again for all the help!
#9

[eluser]Iván Argulo[/eluser]
I'm sorry I didn't help you. I'll look for an answer!

Good luck!




Theme © iAndrew 2016 - Forum software by © MyBB