PHPValidatorは、PHPアプリケーションのデータ検証用の最新のPHPライブラリです。事前定義されたルールを使用してデータを検証するため、またはカスタム検証ルールを作成する柔軟で拡張可能な方法を提供します。
Composerを使用して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には、必要な、文字列、電子メール、maxlengthなどの事前定義された検証ルールが付属しています。
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 ' ,アルファ数値ルール
' 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 ();
}この例では、パスワードがconfism_passwordに等しいかどうかをチェックするCustomPassWordRuleを作成しました。 Passesメソッドをカスタマイズして、特定の検証ロジックを実装できます。
phpvalidatorに貢献したい場合は、貢献ガイドラインに従ってください。
サポートのために、[email protected]にメールで連絡できます。ご質問がある場合、またはphpvalidatorのサポートが必要な場合は、お気軽にお問い合わせください。
PHPValidatorは、MITライセンスに基づいてライセンスされているオープンソースソフトウェアです。