hispanicode / validation
具有出色工具的客户端和服务器端表单验证模型,支持php和jQuery。
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-23 13:20:34 UTC
README
此类可用于在浏览器和服务器端验证表单。
它可以接收一组验证规则,并为给定表单字段生成执行浏览器端验证的JavaScript。
该类可以使用jQuery生成验证JavaScript代码,并在不同表单事件上执行,例如表单输入更改、表单失去焦点、按键释放等。
它还可以在服务器端使用PHP类代码对提交的表单输入执行验证。目前它可以执行以下类型的验证:必需值、最小长度、最大长度、必需文件、文件最小和最大大小、文件MIME类型、图片最大宽度和高度、选中的复选框、有效的日期和时间格式、IP地址、有效的电子邮件地址、有效的URL、匹配正则表达式、最大和最小长度、值等于另一个输入、有效数字、最小和最大数字等。
重要:对于客户端验证,必须包含jQuery和文件validation.js
使用composer安装
composer require hispanicode/validation
简单用法
要求类文件
require "src/Validation.php"; /* if install with composer require "vendor/autoload.php"; */
创建新实例
use Hispanic\Validation; $validation = new Validation();
规则
$rules = array( "name" => "required|name|min_length:3|max_length:50", "email" => "required|email|min_length:6|max_length:80", "password" => "required|between:6-30", "confirm_password" => "required|equalsTo:password", "terms" => "checked", );
可选。自定义消息。例如,翻译成其他语言(法语)
$messages = array( "name.required" => "Le champ :attribute est obligatoire", /* ... */ );
默认消息的多种语言版本可以在language.php文件中找到。西班牙语和英语翻译是可用的,但您可以翻译成您的语言。
//The default translate is "en" in this case not is need use the translate method. //Translate in spanish $validation->translate("es");
在客户端验证中可以处理JavaScript事件
$events = array( "name" => "keyup|blur|change", /* ... */ );
开始客户端验证
//The $messages and $events params are optionals $validation->client($rules, $messages, $events);
生成客户端验证很简单
//The argument is the id of the form echo $validation->getClientValidation("#form");
要获取客户端消息,请设置以下结构,基于bootstrap css样式
<form method="post" id="form"> <div class="form-group"> <label for="name">Name:</label> <input type="text" name="name" id="name" class="form-control" value="" /> <p id="error_name"></p> </div> ... </form>
对于服务器端是server()方法
use Hispanic\Validation;
$validation = new Validation();
//Is possible change the labels attributes with the attribute() method $attributes = array( "name" => "Name", "confirm_password" => "Confirm Password", /* ... */ );
$validation->attributes($attributes);
$rules = array( "name" => "required|regex:/^[a-z]+$/|min_length:3|max_length:50", /* ... */ );
/* if you like the client validation is easy */ $validation->client($rules);
/* request */ if (isset($_POST["name"])) { $validation->server($rules); //The second argument is optional for the custom messages array
//If is valid if ($validation->isValid()) { /* ok / } else { / error */ //get associative array with all errors $errors = $validation->getErrors(); //get only one error, the first error. $first_error = $validation->getFirstError(); } }
验证规则选项
- required : 字段是必需的
- checked : 字段需要被选中
- min_length : 字符串中的最小字符长度。示例:min_length:3
- max_length : 字符串中的最大字符长度。示例:max_length:30
- min : 最小数值。示例:min:1
- max : 最大数值:示例:max:10
- between : 允许的字符范围。示例:between:3-20
- range : 允许的数值范围。示例:range:1-10
- name : 只允许a-záéíóúàèìòùäëïöüâêîôûñ\s。 (忽略大写)
- alpha : 只允许a-záéíóúàèìòùäëïöüâêîôûñ (忽略大写)
- alphanumeric : 只允许0-9a-záéíóúàèìòùäëïöüâêîôûñ (忽略大写)
- digit : 只允许数字
- email : 只允许有效的电子邮件
- ip : 只允许有效的IP
- url : 只允许有效的URL
- date : 只允许有效的日期格式。示例:date:Y-m-d
- time : 只允许有效的时间格式。示例:time:H:i:s
- datetime : 只允许有效的日期时间格式。示例:datetime:Y-m-d H:i:s
- regex : 正则表达式过滤器。示例:regex:/^[a-z]$/i
- equalsTo : 字段值等于其他字段。示例:equalsTo:password
- float : 只允许浮点值
- 整数:仅允许整数值
- 数值:仅允许数值
- 包含:字段需要包含以下所需值之一。示例:包含:one,two,three
- file_required:输入文件是必需的
- min_files:允许的输入文件多个中的最小文件数量。示例:min_files:2
- max_files:允许的输入文件多个中的最大文件数量。示例:max_files:10
- file_min_size:允许的文件最小大小。示例 1MB:file_min_size:1048576
- file_max_size:允许的文件最大大小。示例 1024字节:file_max_size:1024
- mime:允许的文件MIME类型。示例:mime:pdf,txt,js
- img_min_width:允许的图像文件最小宽度。示例 250px:img_min_width:250
- img_max_width:允许的图像文件最大宽度。示例 1024px:img_max_width:1024
- img_min_height:允许的图像文件最小高度。示例 250px:img_min_height:250
- img_max_height:允许的图像文件最大高度。示例 1024px:img_max_height:1024