承接 ygreis/laravel-validators 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

ygreis/laravel-validators

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

ygreis/laravel-validators 适用场景与选型建议

ygreis/laravel-validators 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 989 次下载、GitHub Stars 达 11, 最近一次更新时间为 2024 年 04 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「package」 「php」 「laravel」 「laravel validators」 「custom validators」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ygreis/laravel-validators 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ygreis/laravel-validators 我们能提供哪些服务?
定制开发 / 二次开发

基于 ygreis/laravel-validators 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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