bdbch / vivalidator
简单的输入验证数据验证库
0.2.1
2018-10-21 10:34 UTC
README
Vivalidator
一个简单易用的输入和数据验证库
为什么?
我不喜欢使用Wordpress插件或重量级的PHP包来开发Wordpress表单。我讨厌第三方代码给页面添加标记或因为外部代码的限制而缩小我的可能性。这就是为什么我开始寻找易于使用的验证框架,但没有找到任何完全符合我需求的东西。这就是我开始开发Vivalidator的原因。
安装
Composer方法
在你的项目文件夹中运行 composer require bdbch/vivalidator 以通过Composer安装Vivalidator。它将被自动放置到你的autoload中。
手动方法
确保将所有必要的文件手动复制到你的项目中。从你的代码中的 src 文件夹中引入所需的库以访问Vivalidator类。
使用
使用验证器很简单。查看以下示例代码以了解如何使用此功能。
<?php // Make sure to use the feature in your component use Vivalidator\Validator; // Data to validate. I left the job empty on purpose to demonstrate non-valid data $data = [ 'name' => 'Peter', 'age' => '23', 'job' => '' ]; // Validator Options / Rules // Each field can have multiple rules with their own error messages $options = [ 'name' => [ [ 'rule' => 'required', 'error' => 'Please enter your name' ], [ 'rule' => 'minlength', 'value' => 3, 'error' => 'Your name needs to be longer than 3 characters.' ] ], 'age' => [ [ 'rule' => 'required', 'error' => 'Please enter your age' ], [ 'rule' => 'min', 'value' => 1, 'error' => 'You need to be older than 1' ], [ 'rule' => 'max', 'value' => 150, 'error' => 'I would love to believe you, but I will not' ] ], 'job' => [ [ 'rule' => 'required', 'error' => 'Please specify your job' ] ] ]; // Create a new validator instance and pass data and options in $validator = new Validator($data, $options); // Errors will be collected in an array. // If there are no errors, it will be an empty string if (count($validator->errors)) { echo 'You have ' . count($validator->errors) . ' errors in your submission.'; foreach ($validator->errors as $error) { echo 'Error: ' . $error; } } else { echo 'All set, lets submit!'; } // Thats it // You can now do what ever you want with the submitted data // Make sure to escape the data. The validator doesn't do this right now // TODO: This will be a feature so fields can be escaped if needed
添加ReCaptcha
Vivalidator现在支持ReCaptcha!只需指定你的密钥和错误字符串作为额外数据即可立即配置它!确保手动安装ReCaptcha库或通过 composer require google/recaptcha 安装。
// Check this URL if you need a Key // https://www.google.com/recaptcha/admin // Make sure you have a working ReCaptcha library installed, // otherwise this will do nothing $validator = new Validator($data, $options, [ 'recaptcha' => [ 'secret' => 'YOUR_SECRET_KEY_HERE', 'error' => 'Please verify our captcha' ] ]);
规则
required- 检查输入是否不为空minlength- 检查输入是否比特定值长maxlength- 检查输入是否低于特定值email- 检查输入是否为有效电子邮件url- 检查输入是否为URLnumber- 检查输入是否为数字min- 检查输入是否高于定义的值max- 检查输入是否低于定义的值between- 检查输入是否介于两个数字之间(max和min的组合)regex- 检查输入是否与指定的正则表达式匹配
计划中的规则
查看 此问题 了解更多信息
贡献
我欢迎pull请求,并希望能够得到一些支持。我不是100%最好的PHP开发者,但希望可以将这个Flynt功能做得更好,这样我们就不必再受Wordpress插件的束缚了。
许可
此软件受MIT许可证许可。
