the-support-group / validator
此包的最新版本(0.1.1)没有可用的许可信息。
开源 PHP 验证器
0.1.1
2016-04-21 10:30 UTC
Requires
- php: >=5.5.0
- the-support-group/validator-interop: ~0.1
Requires (Dev)
- phpunit/phpunit: ~4.5
- respect/validation: ~1.0
- the-support-group/validation-adaptor: ~0.1
This package is not auto-updated.
Last update: 2024-09-12 00:45:00 UTC
README
让您的应用程序验证变得容易(受 Laravel 验证启发)
页面索引
建议链接
快速开始 🚀
设置验证门面
<?php use Respect\Validation\Validator as RespectValidator; use TheSupportGroup\Common\Validator\Helpers; use TheSupportGroup\Common\ValidationAdaptor\ValidationAdaptor; // Prep up the validator, ideally done using DI. $respectValidator = new RespectValidator(); $errorBag = new Helpers\FieldsErrorBag(); // Note that any library can be used as long as it is accepted by the ValidationAdaptor. $validationProvider = new ValidationAdaptor($respectValidator); $validationResultProcessor = new Helpers\ValidationResultProcessor($errorBag); $rulesFactory = new Helpers\RulesFactory(); // Create the validation facade that will give us our validation object to work with. $validationFacade = new ValidatorFacade( $validationProvider, $validationResultProcessor, $rulesFactory );
验证数据
<?php // Prepare the input data. $inputData = $_POST; // Prepare rules to be applied on the input data. $rules = [ # firstname and lastname must exists # they should be alphanumeric # atleast 2 characters 'firstname, lastname' => 'required|alpha|min:2', # max until 18 characters only 'lastname' => 'max:18', # must be an email format # must be unique under 'users' table 'email' => 'email|unique:users', # must be numeric # must exists under 'users' table 'id' => 'numeric|exists:users', 'age' => 'min:16|numeric', 'info[country]' => 'required|alpha', # roll[0] or roll[1] values must be in the middle 1 to 100 'roll[0], roll[1]' => 'numeric|between:1, 100', # the format must be 'm-Y.d H:i' 'date' => 'dateFormat:(m-Y.d H:i)', # it must be an image format under $_FILES global variable 'profileImg' => 'image', # the provided phone number should follow the format # correct: +38(123)456-12-34 # wrong: +38(123)56-123-56 # wrong: +39(123)456-12-34 'phoneMask' => 'phoneMask:(+38(###)###-##-##)', 'randNum' => 'between:1, 10|numeric', # the value must be an IP Format 'ip' => 'ip', 'password' => 'required|min:6', # the value from a key 'password' must be equal to 'password_repeat' value 'password_repeat' => 'same:password', # it must be a json format 'json' => 'json', 'site' => 'url', # cash10 or cash25 must only have these # 1 or 2 or 5 or 10 or 20 or 50 'cash10, cash25' => 'in:1, 2, 5, 10, 20, 50', # the value must not have 13 or 18 or 3 or 4 'elevatorFloor' => 'notIn:13, 18, 3, 4', ]; // Custom error messages for rules in case validation does not pass. $customMessages = [ 'info[country].alpha' => 'Only letters please', 'email.required' => 'Field :field: is required', 'email.email' => 'Email has bad format', 'email.unique' => 'This email :value: is not unique', 'elevatorFloor.notIn' => 'Oops', ]; // Run validation on input data. $validationResult = $validationFacade->validate($inputData, $rules, $customMessages);
验证结果对象上的可用方法
// Check if there are any errors. $validationResult->hasErrors(); // Count number of errors. $validationResult->count(); // Get all errors. $validationResult->getErrors(); // Get error for a specific field. $validationResult->getErrors('username'); // Get raw error messages with keys. $validationResult->getRawErrors(); // Get only the first error message for each field. $validationResult->firsts(); // Get the first error message for a specific field. $validationResult->first('username'); // Appending an error message. $validationResult->fieldsErrorBag->add($fieldName, $message);
错误消息变量。
您可以在错误消息中使用 3 个变量,这些变量将动态替换为当时使用的实际值。这些是
:field: => The field being validated.
:rule: => The rule being applied.
:value: => The value being validated against.
贡献 
亲爱的贡献者,该项目刚刚开始,还不稳定,我们非常欢迎您的分支请求。
测试
此项目大部分是基于单元测试的。
测试套件可以在您的计算机上运行。主要依赖项是 PHPUnit,可以使用 Composer 安装。
# run this command from project root
$ composer install --dev --prefer-source
vendor/bin/phpunit --configuration phpunit.xml --coverage-text
有关更多信息,请参阅 PHPUnit 命令行测试运行器。
许可
PHP 数据验证器是开源软件,许可协议为 GNU GPL。