struktal/struktal-validation
最新稳定版本:2.1.2
Composer 安装命令:
composer require struktal/struktal-validation
包简介
PHP library to validate user inputs for APIs or forms
关键字:
README 文档
README
This is a PHP library for validating user inputs through APIs or forms.
Installation
To install this library, include it in your project using Composer:
composer require struktal/validation
Usage
Basic validation
To validate a variable against a set of rules, you can best use the ValidationBuilder class:
use struktal\validation\ValidationBuilder; use struktal\validation\ValidationException; $userInput = $_POST["input"]; // Create a validation builder instance $validatedData = (new ValidationBuilder()) // Define validation rules ->withErrorMessage("Input is missing") ->string() ->withErrorMessage("Input must be a string") ->minLength(5) ->withErrorMessage("Input must be at least 5 characters long") ->maxLength(10) ->withErrorMessage("Input must be at most 10 characters long") // Validate the input ->validate($userInput, function($e) { // Handle validation errors echo "Validation failed: " . $e->getMessage(); }); // Do something with the validated data
Array validation
You can also validate arrays in a similar way:
use struktal\validation\ValidationBuilder; use struktal\validation\ValidationException; $userInput = $_POST; $validatedData = (new ValidationBuilder()) ->withErrorMessage("No POST data provided") ->array() ->children([ "input" => (new ValidationBuilder()) ->withErrorMessage("Input is missing") ->string() ->build(), // More rules could apply, also with explicit error messages "moreInput" => (new ValidationBuilder()) ->withErrorMessage("More input is missing") ->int() ->minValue(0) ->maxValue(10) ->build(), ]) ->withErrorMessage("Invalid POST data") ->validate($userInput, function($e) { // Handle validation errors echo "Validation failed: " . $e->getMessage(); }); // Do something with the validated data
Database validation
If you use Struktal/struktal-orm, you can also check whether an object exists in the database.
Use the inDatabase() method to validate that the user has provided a valid object ID.
The method takes a GenericEntityDAO object, as well as an optional array for additional filter options as parameters.
Dependencies
This library uses the following dependencies:
- ext-pdo
- struktal-orm - GitHub: Struktal/struktal-orm, licensed under MIT license
- pest - GitHub: pestphp/pest, licensed under MIT license
License
This software is licensed under the MIT license. See the LICENSE file for more information.
统计信息
- 总下载量: 821
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-22