stantabcorp/validator

一个PHP验证库

1.3.0 2024-06-12 20:57 UTC

This package is auto-updated.

Last update: 2024-09-12 21:28:01 UTC


README

一个简单的PHP验证库

PHP Composer

安装

composer require stantabcorp/validator

使用库

$validator = new \Stantabcorp\Validator\Validator(["array" => ["to" => "validate"]]); // Init the library providing an array to validate.
$validator->required("array.to"); // Test if the key `to` in the array `array` is present.
$validator->array("array"); // Test if the key `array` is an array.

$validator->isValid(); // Return a boolean is the array is valid or not.

$validator->getErrors(); // Return a list of string containing the error messages.

自定义验证

$validator->customValidation("array", function (\Stantabcorp\Validator\CustomValidator $customValidator) {
    $customValidator->getKey(); // The key (`array` in this case)
    $customValidator->getValue(); // The associated value
    
    // Mark the kay as invalid and add an error message.
    $customValidator->addError("This is an error message");
});

测试

composer run-script test