Reputation: 13
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.
Upvotes: 0
Views: 279
Reputation:
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"]]);
Upvotes: 1