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许可证许可的开源软件。