devknown / request-validator
简单的PHP请求验证器
1.0.0
2022-07-02 21:22 UTC
Requires
- php: >=5.4
- respect/validation: 1.0.*
Requires (Dev)
- phpunit/phpunit: ^4.8
README
使您的应用验证轻松(受Laravel验证启发)
原始项目:progsmile/request-validator。此版本添加了Mysqli适配器。
页面索引
建议链接
快速开始 🚀
<?php $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', ]; $customMessage = [ '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', ]; $v = V::make($_POST, $rules, $customMessage); # for specific field # you can use below code. $v->lastname->passes(); $v->lastname->fails(); # if you're required to check everything # and there must no failing validation $v->passes(); $v->fails(); # get first error message $v->first(); # get first error for `firstname` $v->first('lastname'); $v->firstname->first(); # return first error message from each field $v->firsts(); # get all messages (with param for concrete field) $v->messages(); $v->messages('password'); # get all `password` messages $v->password->messages(); # get 2d array with fields and messages $v->raw(); # to append a message $v->add('someField', 'Something is wrong with this');
贡献
亲爱的贡献者,该项目刚刚开始,尚不稳定,我们很高兴收到您的分支请求。
测试
此测试套件使用Travis CI进行每次运行。每次向此存储库推送的提交都将排队构建到持续集成服务中,并运行所有测试以确保一切顺利且项目稳定。
测试套件可以在您的机器上运行。主要依赖项是PHPUnit,可以使用Composer安装。
# run this command from project root
$ composer install --dev --prefer-source
还需要MySQL数据库进行几个测试。按照以下说明创建数据库
echo 'create database valid charset=utf8mb4 collate=utf8mb4_unicode_ci;' | mysql -u root cat tests/dist/schema.sql | mysql valid -u root
对于这些测试,我们使用没有密码的用户root
。您可能需要在tests/TestHelper.php
文件中更改此设置。
一旦数据库创建完成,请在终端运行测试
vendor/bin/phpunit --configuration phpunit.xml --coverage-text
有关更多信息,请参阅PHPUnit 命令行测试运行器。
许可
PHP请求验证器是开源软件,许可协议为GNU GPL。