0

I want to properly check the user data from a form in PHP. I'm using filter class. I need to use a callback function to check the password. To achieve this, I used this tutorial on twentieths point (callback filter): http://www.phpro.org/tutorials/Filtering-Data-with-PHP.html#21

However I still get this error:

"Warning: filter_var(): First argument is expected to be a valid callback" error.

Here is my class :

class Callback {

private function validPasswd($sPasswd) {

        $r1 = '/#[A-Z]+#/';  //at least one uppercase
        $r2 = '/#[a-z]+#/';  //at least one lowercase
        $r3 = '/#[!@#$%^&*()\-_=+{};:,<.>]+#/';  // at least one special char
        $r4 = '/#[0-9]+#/';  //at least one number

        if (preg_match($r1, $sPasswd)) {
            return false;
        }
        if (preg_match_all($r2, $sPasswd)) {
            return false;
        }
        if (preg_match_all($r3, $sPasswd)) {
            return false;
        }
        if (preg_match_all($r4, $sPasswd)) {
            return false;
        }
        if (strlen($sPasswd) < 8) {
            return false;
        }
        return $sPasswd;
    }
}

and here's the part of my method:

    public function createAccountAction() {
    //get password from form to verification.
    $sPasswd = $_POST['password'];

    //prepare all values to be checked with a filter
    $aOptions = array(
        'first-name' => FILTER_SANITIZE_STRING,
        'last-name' => FILTER_SANITIZE_STRING,
        'login' => FILTER_SANITIZE_STRING,
        'password' => array(
            'filter' => FILTER_CALLBACK,
            'options' => 'validPasswd'
        ),
        'email' => FILTER_SANITIZE_EMAIL,
        'conf-email' => FILTER_SANITIZE_EMAIL
    );
    var_dump(filter_var($sPasswd, FILTER_CALLBACK, array("options" => array("Callback"=>"validPasswd"))));

namespace and use are correct. I think it's the way I call the method that is not good.

4
  • change array("Callback"=>"validPasswd") to array("Callback", "validPasswd") Commented Sep 22, 2015 at 20:33
  • I just tried it but still the same error. Commented Sep 22, 2015 at 20:53
  • try array("options" => array("Callback"=>array($this, 'validPasswd'))) Commented Sep 22, 2015 at 21:04
  • Still not. Do you think that the sub-array in $aOptions is well written ? Commented Sep 22, 2015 at 21:28

1 Answer 1

1

You should be giving the instance that the method should be called on and the method to be called.


If the createAccountAction method is defined in Callback:

return filter_var($sPasswd, FILTER_CALLBACK, ["options" => [$this, "validPasswd"]]);


If the createAccountAction method is defined elsewhere:

// Give Callback::validPasswd public access and...
return filter_var($sPasswd, FILTER_CALLBACK, ["options" => [new Callback, "validPasswd"]]);
Sign up to request clarification or add additional context in comments.

1 Comment

This is it ! Thank you. And it's logical, but it seems that the tutorial linked above has something wrong at this chapter...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.