hilalahmad/supervalidator

SuperValidator 包是一个强大的 PHP 类,旨在简化并增强网络应用程序中表单数据的验证。它提供了一个灵活且可扩展的框架,用于定义验证规则并生成有意义的错误信息。

1.0.0 2024-01-14 12:09 UTC

This package is auto-updated.

Last update: 2024-09-14 14:03:58 UTC


README

SuperValidator 包是一个强大的 PHP 类,旨在简化并增强网络应用程序中表单数据的验证。它提供了一个灵活且可扩展的框架,用于定义验证规则并生成有意义的错误信息。

特性

  • 可扩展的规则系统:轻松定义和扩展各种类型数据的验证规则,如字符串、电子邮件、密码、日期等。

  • 自定义错误信息:为每个验证规则自定义错误信息,允许您提供清晰且用户友好的反馈。

  • 支持常见验证场景:包括内置验证规则,用于常见场景,如必填字段、字符串长度、电子邮件格式、密码强度等。

  • 规则组合:通过组合每个字段的多个规则来构建复杂的验证场景。

  • 数据验证:将表单数据与预定义的规则集进行验证,确保数据完整性和安全性。

入门指南

安装

通过 Composer 安装 SuperValidator

composer require hilalahmad/supervalidator
1. required
2. email
3. password
4. string
5. boolen
6. url
7. integer
8. number
9. date
10. min:length
11. max:length
12. image:1024px // depend on your size
13. Much more with custom validation message
<?php

require './vendor/autoload.php';

use Hilalahmad\Supervalidator\SuperValidator;


$customErrorMessages = [
    'email.required' => 'email field is required.',
    'email.email' => 'Invalid email address.',
    'password.required' => 'Password is required.',
    'password.confirmed:password_confirmation' => 'Password confirmation does not match.',
    'password.password' => 'Custom error message for the password.',
];
$validator = new SuperValidator($customErrorMessages);

$data = [
    'url' => ['required', 'date'],
    'email' => ['required', 'email'],
    'password' => ['required', 'confirmed:password_confirmation', 'password'],
];
$errors = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Assuming you have the SuperValidator class loaded and instantiated

    if (!$validator->validate($data)) {
        $errors = $validator->getErrors();
    } else {
        // Validation successful, process the form data
        $name = $_POST['name'];
        $email = $_POST['email'];   
        $password = $_POST['password'];

        echo "Form submitted successfully!";
    }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sample Form</title>
</head>

<body>
<form action="" method="post">
    <label for="name">Name:</label>
    <input type="text" id="url" name="url">
    <?php
    if (isset($errors['url'])) {
        echo $errors['url'];
    }
    ?>
    <br>

    <label for="email">Email:</label>
    <input type="email" id="email" name="email">
    <?php
    if (isset($errors['email'])) {
        echo $errors['email'];
    }
    ?>
    <br>

    <label for="password">Password:</label>
    <input type="password" id="password" name="password">
    <?php
    if (isset($errors['password'])) {
        echo $errors['password'];
    }
    ?>
    <br>

    <label for="password_confirmation">Confirm Password:</label>
    <input type="password" id="password_confirmation" name="password_confirmation">

    <br>

    <input type="submit" value="Submit">
</form>

</body>

</html>