eboybasit / validator
dev-master
2021-09-18 18:29 UTC
This package is auto-updated.
Last update: 2024-09-19 01:10:53 UTC
README
这是一个验证包,可以轻松且无缝地验证您的表单数据。这虽然不多,但在某些时候可能很有益。
安装
要使用此包,您必须在您的机器上安装php版本7或更高版本
- 首先将此包安装到您的机器上
- 打开命令提示符或终端,并在shell中输入以下命令
composer require eboybasit/validator
用法
步骤 1.
将以下文件导入到您的项目中
require_once 'vendor/autoload.php';
步骤 2.
接下来,假设数据变量包含从您的服务器以关联数组形式的数据,然后您可以通过以下两种方式之一验证您的数据
请注意,两个数组的大小,即数据数组和验证规则数组应该相同。
$data = [ 'email' => 'example@gmail.com', 'email_confirm' => 'example@gmail.com', 'phone' => '9622428688', 'num' => 12, 'color' => '#ff98ff' ]; $rules = [ 'required|email', 'required|matches:email' , 'required|phone', 'gte:13', 'hex|minmax:3-6' ];
数据验证可以通过以下两种方式之一完成。
$validation = App::validate($data, $rules); if( $validation->passes() ){ // write here logic if the validation test passed. } else { // write here the logic for validation test failure. }
或者
if( validate($data, $rules)->passes() ){ // write here logic if the validation test passed. } else { // write here the logic for validation test failure. }
步骤 3.
您还可以设置自定义错误消息或依赖默认错误消息。
在messages.php中,在自定义键下创建针对不同类型规则的自己的错误消息。
请注意,自定义消息数组中的规则名称应与默认数组中的规则名称匹配,以便正确覆盖默认消息
例如
'custom' => [ 'hex' => 'Please enter a 3 digit or 6 digit valid hex code', 'minmax' => //your message here, //rulename => custom error message ];
步骤 4.
您可以使用此函数检索错误。此函数调用返回所有验证错误。
$errors = errors()->all();
该规范提供的各种验证规则有
It ensures that the field passed contains some data or is set or is not null.
It ensures that the field passed is of the type email and does not start with a number
It ensures that this attributes value should match the other attributes values which is specified after colon
It ensures that the value passed is less than a certain number which is specified after colon
It ensures that the value passed is less than or equal to a certain number which is specified after colon
It ensures that the value passed is greater than a certain number which is specified after colon
It ensures that the value passed is greater than or equal to a certain number which is specified after colon
It ensures that the field data length should be aleast a number which is specified after colon
It ensures that the field data length should be atmost a number which is specified after colon
It ensures that the field data length should be aleast a number and should not be more than a number which is specified after colon separated by a hyphen
It ensures that the field data should be a valid phone number
It ensures that the field data should be a valid hex color. Either 3 digits or 6 digits hex values are allowed.