littleskinchina/laravel-email-username-validator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

littleskinchina/laravel-email-username-validator

最新稳定版本:v1.0.2

Composer 安装命令:

composer require littleskinchina/laravel-email-username-validator

包简介

Domain-specific validation for email local-part (username) in Laravel.

README 文档

README

Domain-specific validation for email local-part (username) in Laravel. App developers register validator classes that declare their applicable domains, and the package plugs into Laravel's Validator via a simple email_username rule.

Might be useful to reduce hard-bounced emails caused by invalid email addresses entered by user, might not.

90% codes (including this README) are generated by GitHub Copilot. Use at your own risk. PRs welcome.

Install

composer require littleskinchina/laravel-email-username-validator

Publish the config:

php artisan vendor:publish --tag=config --provider="LittleSkin\\LaravelEmailUsernameValidator\\EmailUsernameValidatorServiceProvider"

Configure

Edit config/email-username-validator.php and list your rule classes:

return [
    'rules' => [
        App\EmailUsernameRules\GmailRule::class,
        App\EmailUsernameRules\OutlookRule::class,
    ],
    // More config options available, see comments on config file for details
];

A rule class must implement LittleSkin\\LaravelEmailUsernameValidator\\Contracts\\EmailUsernameRule:

use LittleSkin\LaravelEmailUsernameValidator\Contracts\EmailUsernameRule;

class ExampleRule implements EmailUsernameRule
{
    // Domains this rule applies to, exact or wildcard
    public static function domains(): array
    {
        return [
            'example.com',
            '*.example.com',
        ];
    }

    public function passes(string $username, string $domain, array $parameters = []): bool
    {
        // Example: require at least 6 chars length
        return strlen($username) >= 6;
    }
}

A single rule can cover multiple domains via its domains() return value.

You can also implement EmailUsernameRuleWithMessage to provide rule-specific failure messages:

class ExampleRuleWithMessage extends ExampleRule implements EmailUsernameRuleWithMessage {
    public function message(): string
    {
        return 'The email address is invalid.';
    }
}

Predefined rules

The package includes some predefined rules for common email providers under namespace LittleSkin\LaravelEmailUsernameValidator\Rules. See src/Rules for details.

Some predefined rules might be too relaxed or too strict. Feel free to modify or create your own rules as needed.

Again, PRs are welcome.

Use in validation

Suggested to use with Laravel's built-in email rule, which validates overall email format and DNS records.

$request->validate([
    'email' => ['required', 'email:strict,dns', 'email_username'],
]);

Semantics:

  • Each domain resolves to at most one rule (the first concrete match wins, otherwise the first wildcard match).
  • If no rules match a domain, behavior is controlled by unknown-domain-pass.

Customizing messages and localization

  • Implement EmailUsernameRuleWithMessage to provide a rule-specific failure message. That message wins over config/localization.
  • Otherwise the package uses the 'messages' config entry, resolved via Laravel's translator if it matches a translation key.
  • Default translations ship under the namespace email-username, or you can point to your own key or literal.
  • The :attribute and :domain placeholders will be replaced automatically by the Validator replacer.

Copyright

Copyright (c) 2026-present Suzhou Honoka Techonology Co., Ltd.

Licensed under MIT.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固