Welcome Guest, Not a member yet? Register   Sign In
setting the $field variable in a custom callback
#1

[eluser]Bramme[/eluser]
Hello all

I'm trying to make a custom callback to validate file uploads. Because I have several file upload inputs in my form, I have to be able to get the input's name in my callback.

I thought I would solve this like this:

One of the arrays in my form_validation config file:
Code:
array(
    'field' => 'part_img',
    'label' => 'post image',
    'rules' => 'callback__file_selected[part_img]'
)

My callback looks like this:
Code:
function _file_selected($str, $field)
{
    if($_FILES[$field]['error'] == 4)
    {
        $this->form_validation->set_message('_file_selected', 'You must specify a file for the %s field.' );
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}
However, this wasn't working. When I started testing, I noticed $field remained empty. Is this a bug, a limitation to custom callbacks or am I doing something wrong?

Note: I tried moving the callback to MY_Form_validation, but now it's not doing anything... (ofcourse I removed the callback_ part from the config file.)
#2

[eluser]wabu[/eluser]
Can you add an additional parameter to a callback like that?

I was checking this out but have to run for now (off to the Apple Store for my battery issue). Back in a little while.
#3

[eluser]Bramme[/eluser]
well, if you look at the callback "matches[fieldname]" and then at the function in the form_validation library, it shows
function matches($str, $field).

Maybe i'm not looking good enough and I'm missing something.
#4

[eluser]wabu[/eluser]
Back with new battery (yay).

If I'm reading it properly, _execute() in the Form Validation library parses custom rules with a regex that matches real "word" characters and thus excludes the bracketed stuff.

I guess custom rules aren't designed to be used that way (while a few of the inbuilt rules accept parameters).
#5

[eluser]Bramme[/eluser]
I just can't figure it out why it's not working.

When I do
Code:
$array = func_get_args();
            print_r($array);
in my callback function (in my controller), I get this:

Code:
Array
(
    [0] =>
    [1] =>
)

So two params ARE being sent to the function (even when I changed the function to function _file_selected($str) {}), but they're both empty. This is mind boggling.

More mind boggling stuff: I only get the empty array at the beginning of my html when I ONLY set the callback in the rules, the moment I use "required" too, it won't print the array anymore.
#6

[eluser]wabu[/eluser]
I think the params are these, passed when your custom rule gets called:

Code:
$result = $this->CI->$rule($postdata, $param);

I think they're empty for the custom rule but work properly with the default rules. I dunno, it's all in the library.

Can I interest you in writing your own validation? Wink
#7

[eluser]Bramme[/eluser]
I'd like to figure out the CI validation before writing my own :p

Shit got weirder: I was adding debug log messages to see what happened where. When I add a log message in my callback _file_selected in my controller, I see it in the logs.

For testing purposes, I added a "matches[field]" rule to some other inputs and added a log_message('debug', 'matching tested') to the function matches() in the Form_validation library. Nothing showed up.

Apparently, that's because it first checks the required rule, if that isn't matched, it just skips all the others. Which makes sense.
#8

[eluser]wabu[/eluser]
"When I add a log message in my callback _file_selected in my controller, I see it in the logs."

As far as I can tell the function is called properly so the log message should appear, but the bracketed parameter added in the rule's name is ignored when the library parses the name out (against the regex mentioned above, so 'callback__file_selected[part_img]' becomes '_file_selected' by the time the rule is run).

It looks like it can't be used that way . . .
#9

[eluser]Bramme[/eluser]
Yup, and I figured out why ^^

On line 487, there's a preg_match that checks non required fields if they have a callback, it looks like this:
Code:
if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))

That \w "matches any word character (alphanumeric & underscore)". That cut off the param right away. I changed it into

Code:
if (preg_match("/(callback_.+)/", implode(' ', $rules), $match))

and now it works ^^

It's a quick and easy fix that might leave some security issues, it's possible a better regex should be written.

Maybe something along the lines of
Code:
preg_match("/(callback_[a-zA-Z0-9_\[\])/"...

No idea though if uppercase letters and or numbers are allowed in a callback (or function name, for that matter).




Theme © iAndrew 2016 - Forum software by © MyBB