ygrees/laravel-validators

为 Laravel 框架提供的自定义验证器

0.5 2024-04-16 11:56 UTC

This package is auto-updated.

Last update: 2024-09-16 13:03:47 UTC


README

为 Laravel 框架提供的自定义验证器

📝 摘要

入门

安装

要求

1:首先,您可以使用 Composer 将 Laravel Validators 作为依赖项安装到您的 Laravel 项目中

composer require ygreis/laravel-validators

如何使用?

基本用法

此软件包提供了两个简单的函数,可以用来返回错误,一个是不返回错误的 makeValidator 函数,另一个是如果发生错误,则会抛出一个自定义异常并返回错误 makeValidatorThrow

验证器文件示例(点击此处)

在文件夹中创建一个文件,例如: \App\Validators\TestValidator

<?php

namespace App\Validators;

use Ygreis\LaravelValidators\AbstractValidator;

class TestValidator extends AbstractValidator
{
    
    public function messages(): array
    {
        return [];
    }

    public function rules(): array
    {
        return [
            'name' => 'required|string',
            'age' => 'nullable|integer|min:18|max:120',
        ];
    }

    public function attributes(): array
    {
        return [
            'name' => 'Name',
            'age' => 'Age',
        ];
    }

    public function validators(): array
    {
        // You can import other validators Here
        return [
            // TestTwoValidator::class
        ];
    }

}

创建验证文件后,请查看以下示例中的使用方法

makeValidator 函数

使用示例

public function TestValidatorException(Request $request)
{
    // Imagine that your Validator is in the path \App\Validators\TestValidator
    $validate = makeValidator(\App\Validators\TestValidator::class, $request->all());
    
    // Check has errors 
    if ($validate->errors()->count()) {
        dd('Get all errors', $validate->errors()->all());   
    }
}

makeValidatorThrow 函数

使用示例

public function TestValidatorException(Request $request)
{
    try {

        makeValidatorThrow(\App\Validators\TestValidator::class, $request->all());

        dd('All right!');

    // Exception from the library where it will be triggered when the Validator fails
    } catch (ValidatorException $exception) {
    
        // $exception->getMessage() Returns the first validation message that did not pass.
        // $exception->getErrors()->messages()->all() Returns all validation messages that did not pass.
        dd($exception->getMessage(), $exception->getErrors()->messages()->all());
    }
}

与请求一起使用

您可以使用 Laravel 的请求来验证数据,也可以使用验证器。

请求文件示例(点击此处)

在文件夹中创建一个文件,例如: \App\Http\Requests\TestRequest

<?php

namespace App\Http\Requests;

use App\Validators\TestValidator;
use Ygreis\LaravelValidators\AbstractRequest;

class TestRequest extends AbstractRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'user.age' => 'nullable|integer|min:18|max:120',
        ];
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'user.age' => 'User Age',
        ];
    }

    public function validators()
    {
        return [TestValidator::class];
    }

}
在控制器中使用请求

在文件夹中创建一个控制器,例如: \App\Http\Controllers

<?php

namespace App\Http\Controllers;

use App\Http\Requests\TestRequest;

class TestController extends Controller {

    public function TestValidatorException(TestRequest $request)
    {
        dd('All Right');
    }

}

当数据无效时,默认情况下会重定向到上一个页面,显示发生的错误,要在视图中捕获错误,您可以这样做

在视图中获取请求错误
@if($errors->any())
    {!! implode('', $errors->all('<div>:message</div>')) !!}
@endif

示例项目

我们提供了一个软件包,您可以测试此处提到的所有示例,只需访问下面的链接即可 :)

点击这里