muhammadsiyab / form_validation
PHP 表单验证库
1.0
2018-08-24 05:35 UTC
This package is auto-updated.
Last update: 2024-09-25 21:43:00 UTC
README
执行 HTML 表单的服务器端验证
下载和实现
- 手动下载 zip 文件并将其包含在项目目录中
或者 (使用 Composer)
运行命令composer require muhammadsiyab/form_validation
- 包含 Form_Validation 库
require_once './vendor/autoload.php';
验证表单
<?php // Instantiate `Form_Validation` Class $form_validation = new \FormValidation\Form_Validation(); // Array containing custom messages // (Optional parameter, if not passed, default error messages will be used) $messages = array( 'required' => '{field} is cumpolsary', 'max_length' => '{field} must be in limit of {limit}', 'min_length' => '{field} must be at least of {limit} characters', 'regex' => '{field} must be in specific pattern' ); // Validation rules $form_validation->validate(array('field_name', 'field_label', 'required|>10|<3', $messages)); // Check whether a record exists in database $form_validation->exists('field_name|field_label', 'localhost|user|password|db_name', 'table_name|column_name', 'custom_error'); // Check if ($form_validation->is_form_ok() === false) { // Custom error markup $form_validation->set_error_markup('<li>', '</li>'); // Showing validation errors $errors = $form_validation->show_validation_errors(); for ($i = 0; $i < count($errors); $i++) { echo $errors[$i]; } } else { // Do something here }
可用方法
1. validate
使用指定的规则验证表单
参数
array
$config 包含验证配置的数组示例
array('field_name', 'field_label', 'validation_rules_separated_with_pipe', array_containing_custom_messages)
2. exists
检查值是否已存在于数据库中
参数
-
string
$field 与字段相关的数据,例如名称和标签示例
'field_name|field_label'
-
string
$db 数据库连接详情,例如主机、用户、密码和数据库名称示例
'localhost|user|password|db_name'
-
string
$table 与表相关的数据,例如表名称和列名称示例
'table_name|column_name'
-
string
$error (可选) 显示的自定义错误
3. set_error_markup
设置验证错误的自定义标记
-
string
$opening_markup 包含打开标记的字符串示例
'<span style="color: red">'
-
string
$closing_markup 包含关闭标记的字符串示例
'</span>'
4. show_validation_errors
@return 类型 Array
(使用循环遍历错误)
显示验证错误
5. is_form_ok
检查验证是否完成且存在错误