Jump to content

Recommended Posts

Hi All,

 

I'm using the php filter functions to validate my form data. For custom filters, I'm using the FILTER_CALLBACK filter but when I'm using files, this never seems to be called? It may be that the filters are not designed to be called for files but I can't find any documentation to support that. Anybody know anything about this?

 

iSE

Link to comment
https://forums.phpfreaks.com/topic/184625-filter_callback-files/
Share on other sites

It may be that the filters are not designed to be called for files but I can't find any documentation to support that.

 

I can't find any documentation that doesn't support that. Looking at the extension, I'm not sure its designed to handle files. What exactly are you trying to filter? Can we see one of your callbacks?

Well that's not surprising as I can find very little documentation on the filters as it is.

 

I add rules, forwhich is gets the filter argument to add to the filter argument array when calling: filter_input_array($this->_inputType, $this->_filterArgs);

 

Everything works fine. The filter argument for each rule object which uses callbacks is:

 

<?php
public function getFilterArgument()
    {
        return array('filter'  => FILTER_CALLBACK,
                     'options' => array($this, 'callbackFunction'));
    }
?>

 

Then the callback function will be (for example, checking the length of a string):

 

<?php
public function callbackFunction($string)
    {
        // Check arguments
        if (!is_string($string)) throw new InvalidArgumentException('Form_Rule_Length::callbackFunction() expects a string as the first argument; ' . gettype($string) . ' given.');
        // Check length
        if (isset($this->_min)) if (strlen($string) < $this->_min) return false;
        if (isset($this->_max)) if (strlen($string) > $this->_max) return false;
        // Must be valid if reached here
        return $string;
    }
?>

 

This all works fine, when I use a callback for a file, i can get to the getFilterArgument() method, but the callback method is NEVER called on filter_input_array.

 

iSE

What I was trying to do was add a dummy $_POST[$fieldName] value hoping this would force the callback method to be called. However, filter_input_array, uses a copy of the input array BEFORE the script is run, so any changes to POST don't reflect in the filter_input_array values to be filtered.

 

I changed from filter_input_array to use filter_var_array, and used the superglobal as the input array. Works fine now. Is a bit annoying that it works this way but what can you do.

 

Thanks for your help guys.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.