mouyong/validate

Based on illuminate/validate, validate is used outside the laravel framework

v2.0.4 2022-11-09 00:57 UTC

This package is auto-updated.

Last update: 2024-10-17 10:05:40 UTC


README

安装

composer require mouyong/validate -vvv

文件翻译创建

./webman plugin:install mouyong/validate

or

php ./vendor/mouyong/validate/src/scripts/install.php

移除翻译文件

./webman plugin:uninstall mouyong/validate

or

php ./vendor/mouyong/validate/src/scripts/uninstall.php

基础用法

$data = [
    'name'  => 'Tinywan',
    'age'  => 24,
    'email' => 'Tinywan@163.com'
];


/** @var \Illuminate\Contracts\Validation\Validator|\Illuminate\Contracts\Validation\Factory $validator */
$validator = validator($data, [
    'name'  => 'require|max:25',
    'age'   => 'require|number|between:1,120',
    'email' => 'require|email'
], [
    'name.require' => '名称必须',
    'name.max'     => '名称最多不能超过25个字符',
    'age.require'   => '年龄必须是数字',
    'age.number'   => '年龄必须是数字',
    'age.between'  => '年龄只能在1-120之间',
    'email.require'        => '邮箱必须是数字',
    'email.email'        => '邮箱格式错误'
]);

$validator->validate();

// 或者直接简写
validator()->validate($data, [
    'name'  => 'require|max:25',
    'age'   => 'require|number|between:1,120',
    'email' => 'require|email'
], [
    'name.require' => '名称必须',
    'name.max'     => '名称最多不能超过25个字符',
    'age.require'   => '年龄必须是数字',
    'age.number'   => '年龄必须是数字',
    'age.between'  => '年龄只能在1-120之间',
    'email.require'        => '邮箱必须是数字',
    'email.email'        => '邮箱格式错误'
]);

更多验证规则请阅读:http://laravel.com/docs/9.x/validation#available-validation-rules

参考

https://medium.com/@jeffochoa/using-the-illuminate-validation-validator-class-outside-laravel-6b2b0c07d3a4