sevada/nie-validator
最新稳定版本:v2.0
Composer 安装命令:
composer require sevada/nie-validator
包简介
Laravel validation rule for Spanish NIE (Número de Identidad de Extranjero) numbers
README 文档
README
A robust Laravel validation package for Spanish identification numbers:
- NIE (Número de Identidad de Extranjero)
- DNI (Documento Nacional de Identidad)
Validates according to the official algorithm regulated by Real Decreto 1553/2005, Artículo 11.
Features
✅ Combined Support - Validates both NIE and DNI numbers
✅ Accurate Validation - Implements official Spanish algorithms
✅ Flexible Input - Accepts various formats (spaces, hyphens, mixed case)
✅ Laravel Integration - Seamless integration with Laravel's validator
✅ Multilingual - Built-in English and Spanish error messages
✅ Type Safe - Strict type declarations throughout
✅ Well Tested - 100+ test cases with comprehensive coverage
✅ Zero Dependencies - No external dependencies beyond Laravel
Requirements
- PHP 8.2 or higher
- Laravel 10.x, 11.x, or 12.x
Installation
Install via Composer:
composer require sevada/nie-validator
The service provider will be automatically registered.
Publishing Configuration (Optional)
Publish the configuration file:
php artisan vendor:publish --tag=nif-config
Publish translation files:
php artisan vendor:publish --tag=nif-translations
Usage
Basic Validation
Use the nie or dni validation rules in your validation logic:
use Illuminate\Http\Request; public function store(Request $request) { $validated = $request->validate([ 'nie_number' => ['required', 'nie'], 'dni_number' => ['required', 'dni'], ]); // Numbers are valid! }
Using Rule Classes
You can also use the rule classes directly:
use Sevada\NifValidator\Rules\Nie; use Sevada\NifValidator\Rules\Dni; $request->validate([ 'nie_number' => ['required', new Nie], 'dni_number' => ['required', new Dni], ]);
Accepted Formats
The validator automatically normalizes input and accepts various formats:
NIE Examples:
'X1234567L' // ✅ Standard format 'Y-1234567-X' // ✅ With hyphens 'Z 1234567 R' // ✅ With spaces
DNI Examples:
'12345678Z' // ✅ Standard format '12345678-Z' // ✅ With hyphens '12345678 Z' // ✅ With spaces
Configuration
The default configuration (config/nif.php):
return [ // Official control letter sequence (DO NOT MODIFY) 'control_letters' => str_split('TRWAGMYFPDXBNJZSQVHLCKE'), // Validation strictness 'strict_format' => false, // Allow spaces and hyphens ];
Translations
English (Default)
The :attribute is not a valid Spanish NIE (Número de Identidad de Extranjero).
The :attribute is not a valid Spanish DNI (Documento Nacional de Identidad).
Spanish
El :attribute no es un NIE (Número de Identidad de Extranjero) válido.
El :attribute no es un DNI (Documento Nacional de Identidad) válido.
To use Spanish translations, set your application locale:
app()->setLocale('es');
Testing
Run the test suite:
composer test
Security
This package validates mathematical format but does not verify authenticity with official databases. See SECURITY.md.
Important: Handle national ID numbers as sensitive personal data (GDPR).
License
The MIT License (MIT). Please see LICENSE for more information.
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-30