定制 ygreis/laravel-validators 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ygreis/laravel-validators

最新稳定版本:0.5

Composer 安装命令:

composer require ygreis/laravel-validators

包简介

Custom validations for the laravel framework

README 文档

README

Custom validations for the laravel framework

📝 Summary

Getting Started

Installation

Requires:

1: First, you may use Composer to install Laravel Validators as a dependency into your Laravel project:

composer require ygreis/laravel-validators

How to use?

Basic usage

The package provides two simple functions that can be used, one where errors are returned makeValidator and the other, if an error occurs, a custom exception is thrown that also returns errors makeValidatorThrow.

Validator file example (click here)

Create a file in a folder, example: \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
        ];
    }

}

After creating a validation file, see how to use it in the examples below

Function makeValidator

Example of use:

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());   
    }
}

Function makeValidatorThrow

Example of use:

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());
    }
}

With Requests

You can use Laravel's Requests to validate the data and you can use Validators too.

Request file example (click here)

Create a file in a folder, example: \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];
    }

}
Usage Request in Controller

Create a controller in a folder, example: \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');
    }

}

When the data is invalid, by default it will redirect to the previous page showing the errors that occurred, to catch the errors in the view you can do something like this:

Get request errors in the view
@if($errors->any())
    {!! implode('', $errors->all('<div>:message</div>')) !!}
@endif

Example project

We provide a package so you can test all the examples mentioned here, just access the link below :)

Click Here

统计信息

  • 总下载量: 798
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-12