定制 timacdonald/pulse-validation-errors 二次开发

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

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

timacdonald/pulse-validation-errors

Composer 安装命令:

composer require timacdonald/pulse-validation-errors

包简介

Validation errors card for Laravel Pulse

README 文档

README

A card for Laravel Pulse to show validation errors impacting users.

Validation errors card for Laravel Pulse

Installation

First, install the package via composer:

composer require timacdonald/pulse-validation-errors

Next, add the recorder to your config/pulse.php:

return [
    // ...

    'recorders' => [
        TiMacDonald\Pulse\Recorders\ValidationErrors::class => [
            'enabled' => env('PULSE_VALIDATION_ERRORS_ENABLED', true),
            'sample_rate' => env('PULSE_VALIDATION_ERRORS_SAMPLE_RATE', 1),
            'capture_messages' => true,
            'ignore' => [
                // '#^/login$#',
                // '#^/register$#',
                // '#^/forgot-password$#',
            ],
            'groups' => [
                // '#^/products/.*$#' => '/products/{user}',
            ],
        ],

        // ...
    ],
];

Next, add the card to your resources/views/vendor/pulse/dashboard.php:

<x-pulse>
    <livewire:pulse.validation-errors cols="8" rows="4" />

    <!-- ... -->
</x-pulse>

Finally, get to improving your user experience. At LaraconUS I gave a talk on how much our validation sucks. If you are here, you likely also care about how your users experience validation errors on your app, so I'd love you to give it a watch.

Features

  • Supports session based validation errors
  • Supports API validation errors
  • Support Inertia validation errors
  • Support Livewire validation errors
  • Supports multiple error bags
  • Fallback for undetectable validation errors (based on 422 response status)
  • Capture generic validation exceptions for custom response types

Ignore specific error messages

You may ignore specific endpoints via the recorders ignore key, however in some situations you may need more complex ignore rules. You can use Pulse's built in Pulse::filter method to achieve this.

Here is an example where we are ignore a specific error message:

use Laravel\Pulse\Entry;
use Laravel\Pulse\Facades\Pulse;
use Laravel\Pulse\Value;

/**
 * Bootstrap any application services.
 */
public function boot(): void
{
    Pulse::filter(fn ($entry): bool => match ($entry->type) {
        'validation_error' => ! Str::contains($entry->key, [
            'The password is incorrect.',
            'Your password has appeared in a data leak.',
            // ...
        ]),
        // ...
        default => true,
    });
}

Capture validation errors for custom response formats

If you are returning custom response formats, you may see __laravel_unknown in the dashboard instead of the input names and error messages. This is because the package parses the response body to determine the validation errors. When the body is in an unrecognised format it is unable to parse the keys and messages from the response.

You should instead dispatch the ValidationExceptionOccurred event to pass the validation messages to the card's recorder. You may do this wherever you are converting your exceptions into responses. This usually happens in the app/Exceptions/Handler:

<?php

namespace App\Exceptions\Handler;

use Illuminate\Support\Facades\Event;
use Illuminate\Validation\ValidationException;
use Laravel\Pulse\Facades\Pulse;
use Throwable;
use TiMacDonald\Pulse\ValidationExceptionOccurred

class Handler
{
    // ...

    public function render($request, Throwable $e)
    {
        if ($e instanceof ValidationException) {
            Pulse::rescue(fn () => Event::dispatch(new ValidationExceptionOccurred($request, $e)));
        }

        // custom exception rendering logic...
    }
}

timacdonald/pulse-validation-errors 适用场景与选型建议

timacdonald/pulse-validation-errors 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 659.26k 次下载、GitHub Stars 达 105, 最近一次更新时间为 2024 年 05 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 timacdonald/pulse-validation-errors 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 659.26k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 105
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 105
  • Watchers: 2
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-10