V2:Custom Types
From PhpInputValidator
If you need to make a custom type it is easy to do.
For the following example we are going to create a type called foo in which we want it to preg_match all digits and _. So for the example we want this regex:
'/^[\d_]/i'
Contents |
Step 1
Create a file class php file with the name of the type.
Note
You can only have 1 _ in the name of the type.
Make sure the extension is .inc.php
Example
class.foo.inc.php
Step 2
In the php file create a phpInputValidator_Type class that has your type name.
You should also add the direct access protection.
Example
<?php
if (!defined('phpInputValidator')) {
die('Direct access is not allowed');
}
class phpInputValidator_Type_Foo {
}
if (!defined('phpInputValidator')) {
die('Direct access is not allowed');
}
class phpInputValidator_Type_Foo {
}
Step 3
Create a public static method called is that returns a bool and has a parameter which is the value to validate.
Note
true = valid type
false = invalid type
Example
<?php
if (!defined('phpInputValidator')) {
die('Direct access is not allowed');
}
class phpInputValidator_Type_Foo {
public static function is($value) {
//Check against the regex
if (!preg_match('/^[\d_]/i', $value)) {
return false;
}
return true;
}
}
if (!defined('phpInputValidator')) {
die('Direct access is not allowed');
}
class phpInputValidator_Type_Foo {
public static function is($value) {
//Check against the regex
if (!preg_match('/^[\d_]/i', $value)) {
return false;
}
return true;
}
}
Step 4
Upload it to /lib/includes/type/
Step 5
That's pretty much it. Now you can call it.
$value = $getvar->get('value', 'POST', 'foo');