0

I am trying to use the CALLBACK function inside the filter_input() and get this error:

Warning: filter_input(): First argument is expected to be a valid callback in C:\xampp\htdocs\TEST.php on line ...

Attempt

<form method="POST" name="textfield" id="textfield" action="" required>
<label for="name">Name:</label><br>
<input type="text" name="name" id="name" maxlength="255" size="25">
<br>
<button type="reset">Reset!</button>
<br>
<button type="submit" name="submit" id="submit">Submit!</button><br>
</form>

<?php
if($_SERVER['REQUEST_METHOD']==='POST')
{
    $name = $_POST['name'];
    
    function FILTER_VALIDATE_STRING($data)
    {
        if(is_string($data))
        {
            $value = TRUE;
        }
        else
        {
            $value = FALSE;     
        }
        GLOBAL $value;
        return $value;
    }
    
    echo FILTER_VALIDATE_STRING($name);

    if(!filter_input(INPUT_POST,"name",FILTER_CALLBACK,
    "FILTER_VALIDATE_STRING"))
    {
        die("Enter a valid STRING!");
    }
}
?>

I am trying to create a custom VALIDATOR in the function filter_input(). So far, we got these built-into php: A.

filter_input(INPUT_POST,"input",FILTER_VALIDATE_URL)

B.

filter_input(INPUT_POST,"input",FILTER_VALIDATE_EMAIL)

The above 2 VALIDATORS exist. But not this following one:

filter_input(INPUT_POST,"input",FILTER_VALIDATE_STRING)

I am aware that, we have the STRING SANITIZER:

filter_input(INPUT_POST,"input",FILTER_SANITIZE_STRING)

But I don't want to sanitize the user's (form inputer's) input. I want to validate it. So now trying to build my own FILTER_VALIDATE_STRING to use inside the filter_input() function. I know there are other ways of validating a string but I want to learn how to build my own function or whatever to use it with the filter_input() function. That's the assignment for now. I would appreciate anyone editing my code as a fix or answer to this question.

  • Constant [FILTER_SANITIZE_STRING](https://stackoverflow.com/questions/69207368/constant-filter-sanitize-string-is-deprecated) is deprecated. Please stop using it. – Dharman Nov 15 '21 at 12:40

1 Answers1

0

I think you're close but you should take a closer look at the examples on the callback page (https://www.php.net/manual/en/filter.filters.misc.php).

The custom callback requires an array with an options key:

filter_var($string, FILTER_CALLBACK, array('options' => 'FILTER_VALIDATE_STRING'));

Note that scalar types are converted to strings with filter_var. I'm not sure about with filter_input.

cOle2
  • 4,725
  • 1
  • 24
  • 26
  • Constant [FILTER_SANITIZE_STRING](https://stackoverflow.com/questions/69207368/constant-filter-sanitize-string-is-deprecated) is deprecated. Please stop using it. – Dharman Nov 15 '21 at 12:40