davebugg/validony

简单而强大的数据验证器

1.0.0 2024-03-02 18:19 UTC

This package is auto-updated.

Last update: 2024-10-01 00:08:52 UTC


README

简单而强大的数据验证器

项目提供数据验证工具。可能性

  • 数组验证
  • 具有相似键的数组验证

Composer

composer require davebugg/validony

使用方法

动态

定义类并设置

$validator = (new Validony(
$_POST,                                             // Array to validate 
\DavesValidator\Validator\Messages::$messages,      // Array with error messages
\DavesValidator\Validator\Messages::$filedNames,    // Array to rename fields in answer
\DavesValidator\Validator\Checker::class,           // Class which contains validation methods
['DavesValidator\\Validator\\Validony', 'AnswerErrorCallback'], // Class and static method to send validation error
'en'));// Language for errors (the keys of ...\Messages::$messages or your Class for messages)

调用它

//в папке Lists, нужно лишь вернуть массив проверки
$validator->ValidateList(
'TimeValidator', //Method to return the validation rules 
false, // Path to your Lists Directory
'DavesValidator\\Validator\\Lists\\', // Namespace of your classes contains in Lists Folder 
false, // Run Callback functions if found\fields with no valid data
true, // Print field's name in error message
true, // Print field's value in error message
false); // Return all errors in one iteration

获取结果和错误

$valid = $validator->isValid(); // valid or not
$errors = $validator->getErrors(
false, // return string || array
true); // return array of fields in errors array if true

具有相似键的数组的验证

$init = [ 
    'password' =>  [C::required, C::password] // Rules
    ];
$_POST = [                                   // Data
    'password_1' => '42',
    'password2' => '42',
    'password_abcd' => 'abcd'
];
$validatorLists = (new ValidateLists($_POST));
$validatorLists->CheckData($init,false,true,true,true);
$valid = $validatorLists->isValid();
$errors = $validatorLists->getErrors(false, true);

静态

静态类不太吸引人使用,但您可以通过参考类内的PHPDoc来使用它。

附加

探索Checker和MainLists类以获得更深入的了解。