ryodevz/validator

验证器。

v1.8.0 2023-11-02 17:53 UTC

This package is auto-updated.

Last update: 2024-10-01 00:14:44 UTC


README

Latest Version

Laravel风格的验证和一些其他功能。

安装验证器

推荐通过Composer来安装验证器。

composer require ryodevz/validator

验证器示例用法

<?php

use Ryodevz\Validator\Facades\Validator;

// Make validation
$validator = Validator::make([
    'name'  => $_POST['name'],
    'email' => $_POST['email'],
], [
    'name'  => 'required|min:3',
    'email' => 'required|email',
]);

// Validate
$validator->validate();

// Check if it passes validation
if(!$validator->fails()) {
    // code . . .
}

// All error messages with key
$validator->errors();

// All error messages without key
$validator->all();

// First error message
$validator->first();

从配置文件中自定义错误信息

config/validator.php中创建一个配置文件并填充以下内容:

<?php

return [
    'array' => 'The :attribute must be an array.',
    'active_url' => 'The :attribute is not a valid URL.',
    'boolean' => 'The :attribute field must be true or false.',
    'confirmed' => 'The :attribute confirmation does not match.',
    'in' => 'The selected :attribute is invalid.',
    'ip' => 'The :attribute must be a valid IP address.',
    'ip4' => 'The :attribute must be a valid IPv4 address.',
    'ip6' => 'The :attribute must be a valid IPv6 address.',
    'integer' => 'The :attribute must be an integer.',
    'max' => [
        'integer' => 'The :attribute must not be greater than :max.',
        'string' => 'The :attribute must not be greater than :max characters.',
        'array' => 'The :attribute must not have more than :max items.',
    ],
    'min' => [
        'integer' => 'The :attribute must be at least :min.',
        'string' => 'The :attribute must be at least :min characters.',
        'array' => 'The :attribute must have at least :min items.',
    ],
    'not_in' => 'The selected :attribute is invalid.',
    'required' => 'The :attribute field is required.',
    'required_with' => 'The :attribute field is required when :required_with is present.',
    'required_without' => 'The :attribute field is required when :required_without is not present.',
    'same' => 'The :attribute and :same must match.',
    'string' => 'The :attribute must be a string.',
    'timezone' => 'The :attribute must be a valid timezone.',
    'url' => 'The :attribute must be a valid URL.'
];

规则

一些可用的规则