PHPVALIDATOR是用於PHP應用程序中數據驗證的現代PHP庫。它提供了一種靈活且可擴展的方法,可以使用預定義的規則或創建自定義驗證規則來驗證數據。
使用作曲家安裝phpvalidator:
composer require blakvghost/php-validator use BlakvGhost PHPValidator Validator ;
use BlakvGhost PHPValidator ValidatorException ;
try {
$ data = [
' username ' => ' BlakvGhost ' ,
' email ' => ' [email protected] ' ,
' score ' => 42 ,
];
// or
// $data = $_POST;
$ validator = new Validator ( $ data , [
' username ' => ' required|string ' ,
' email ' => ' required|email ' ,
' score ' => [ ' required ' , ' max:200 ' , new CustomRule ()],
' password ' => new CustomRule (),
]);
if ( $ validator -> isValid ()) {
echo " Validation passed! " ;
} else {
$ errors = $ validator -> getErrors ();
print_r ( $ errors );
}
} catch ( ValidatorException $ e ) {
echo " Validation error: " . $ e -> getMessage ();
}您還可以自定義驗證錯誤消息或specify the default language
$ data = [
' username ' => ' BlakvGhost ' ,
];
$ validator = new Validator (
$ data ,
[
' username ' => ' required|string ' ,
],
[
' username ' => [
' required ' => ' Votre nom d ' utilisateur doit être présent ' ,
' string ' => ' Votre nom d ' utilisateur doit forcément être une chaîne de caractère ' ,
],
]
);
// For the default language
$ validator = new Validator ( $ data , $ rules , lang: ' fr ' );Predefined Rules :PHPVALIDATOR附帶一組預定義的驗證規則,例如必需,字符串,電子郵件,最大長度等。
Custom Rules :通過實現Rule接口來輕鬆創建自定義驗證規則。
Multilingual Support :使用LangManager根據應用程序的語言自定義驗證錯誤消息。
PHPVALIDATOR提供了各種預定義的規則,您可以將其用於數據驗證。這是一些常用規則的列表以及其用法的示例:
所需的規則
' username ' => ' required '字符串規則
' username ' => ' string '電子郵件規則
' email ' => ' email '最大長度規則
' username ' => ' max:25 '確認的規則
' password_confirmation ' => ' confirmed:password '文件規則
' file ' => ' file '接受的規則
"yes" , "on" , "1"或true 。對複選框有用。 ' terms_and_conditions ' => ' accepted '如果規則接受
' terms_and_conditions ' => ' accepted_if:is_adult,true 'ActiveUrl規則
' website ' => ' active_url '阿爾法規則
' name ' => ' alpha '數字規則
' age ' => ' numeric '小寫規則
' username ' => ' lowercase '大寫規則
' username ' => ' uppercase '在規則中
' role ' => ' in:admin,editor,viewer '無效的規則
null或空。 ' optional_field ' => ' nullable '密碼規則
secure password 。 ' password ' => ' password '相同的規則
' password_confirmation ' => ' same:password '最大長度規則
' username ' => ' min:8 '不統治
' value ' => ' not_in:foo,bar '規則要求
' firstname ' => ' required_with:lastname ' ,有效的IP規則
' client_ip ' => ' ip ' ,JSON規則
' config ' => ' json ' ,URL規則
' website ' => ' url ' ,alpha數字規則
' pseudo ' => ' alpha_num ' ,布爾規則
' is_admin ' => ' bool ' ,尺寸規則
[
' string ' => ' size:7 ' , // strlen(string) == 7
' integer ' => ' size:7 ' , // integer == 7
' array ' => ' size:7 ' , // count(array) == 7
' file ' => ' size:512 ' , // file size (kb) == 512
]規則不需要
' email ' => ' not_required_with:phone_number ' ,除了預定義的規則外,您還可以通過實現Rule接口來創建自定義驗證規則。這是如何創建和使用自定義規則的示例:
// CustomPasswordRule.php
namespace YourNameSpace Rules ;
use BlakvGhost PHPValidator Contracts Rule ;
class CustomPasswordRule implements Rule
{
protected $ field ;
public function __construct ( protected array $ parameters = [])
{
}
public function passes ( string $ field , $ value , array $ data ): bool
{
$ this -> field = $ field ;
// Implement your custom validation logic here
// Example: Check if the password is equal to confirm_password
return $ value === $ data [ ' confirm_password ' ];
}
public function message (): string
{
return " Vos deux mot de passes ne sont pas identiques " ;
}
}直接使用您的自定義課程
use BlakvGhost PHPValidator Validator ;
use BlakvGhost PHPValidator ValidatorException ;
use YourNameSpace CustomPasswordRule ;
// ...
try {
$ data = [
' password ' => ' 42 ' ,
' confirm_password ' => ' 142 ' ,
];
// or
// $data = $_POST;
$ validator = new Validator ( $ data , [
' password ' => [ ' required ' , new CustomPasswordRule ()],
]);
if ( $ validator -> isValid ()) {
echo " Validation passed! " ;
} else {
$ errors = $ validator -> getErrors ();
print_r ( $ errors );
}
} catch ( ValidatorException $ e ) {
echo " Validation error: " . $ e -> getMessage ();
}將您的自定義類添加到規則列表中,然後將其像本機一樣使用
use BlakvGhost PHPValidator Validator ;
use BlakvGhost PHPValidator ValidatorException ;
use BlakvGhost PHPValidator Mapping RulesMaped ;
use YourNameSpace CustomPasswordRule ;
// Add your rule here using an alias and the full namespace of your custom class
RulesMaped:: addRule ( ' c_password ' , CustomPasswordRule::class);
try {
$ data = [
' password ' => ' 42 ' ,
' confirm_password ' => ' 142 ' ,
];
$ validator = new Validator ( $ data , [
' password ' => ' required|c_password ' ,
]);
if ( $ validator -> isValid ()) {
echo " Validation passed! " ;
} else {
$ errors = $ validator -> getErrors ();
print_r ( $ errors );
}
} catch ( ValidatorException $ e ) {
echo " Validation error: " . $ e -> getMessage ();
}在此示例中,我們創建了一個CustomPasswordrule,該WisturePasswordrule檢查密碼是否等於庫_password。您可以自定義通過方法來實現特定的驗證邏輯。
如果您想為PhpValidator做出貢獻,請遵循我們的貢獻指南。
為了支持,您可以通過電子郵件[email protected]與我聯繫。如果您有任何疑問或在PhpValidator上需要幫助,請隨時與我聯繫。
PHPVALIDATOR是根據MIT許可證許可的開源軟件。