Skip to content

Laravel Integration

slashlab/numerik-laravel adds Numerik’s validators as Laravel validation rules. It supports both class-based and string-based syntax and is auto-discovered — no manual registration needed.

  • PHP 8.3 or higher
  • Laravel 11, 12, or 13
  1. Require the package

    Terminal window
    composer require slashlab/numerik-laravel
  2. No configuration needed

    The service provider is auto-discovered. There is no config file to publish or boot step to add.

use SlashLab\NumerikLaravel\Rules\PeselRule;
use SlashLab\NumerikLaravel\Rules\NipRule;
use SlashLab\NumerikLaravel\Rules\RegonRule;
use SlashLab\NumerikLaravel\Rules\KrsRule;
public function rules(): array
{
return [
'pesel' => ['required', new PeselRule()],
'nip' => ['required', new NipRule()],
'regon' => ['required', new RegonRule()],
'krs' => ['required', new KrsRule()],
];
}

Class-based rules return a distinct message for each failure reason. For example, a NIP with an incorrect checksum returns a different message than one with an invalid length.

Error messages resolve the field label from validation.attributes when available, matching the behaviour of Laravel’s built-in rules. If no label is registered, the field name is humanised — underscores replaced with spaces, first letter capitalised.

lang/en/validation.php
'attributes' => [
'nip_number' => 'NIP',
],

The package ships with English (en) and Polish (pl) translations. To publish and customise them:

Terminal window
php artisan vendor:publish --tag=numerik-lang

This copies the translation files to lang/vendor/numerik/. Edit the messages there to override any string.

All rules accept an optional strict parameter (default true). In strict mode, additional semantic checks are applied on top of structural and checksum validation.

For PESEL, strict mode rejects numbers with a future birth date or all-identical digits:

// strict on (default) — rejects future dates and suspicious patterns
new PeselRule()
// strict off — only structural and checksum checks are applied
new PeselRule(strict: false)

The same parameter is accepted by NipRule, RegonRule, and KrsRule.

PeselRule supports additional constraints that are checked after the number passes structural validation.

Reject numbers that do not match an expected gender:

use SlashLab\Numerik\Enums\Gender;
new PeselRule(gender: Gender::Male)
new PeselRule(gender: Gender::Female)

Reject numbers whose encoded birth date falls outside a given range:

new PeselRule(bornBefore: new DateTimeImmutable('2000-01-01'))
new PeselRule(bornAfter: new DateTimeImmutable('1980-12-31'))

Constraints can be combined:

new PeselRule(
gender: Gender::Female,
bornAfter: new DateTimeImmutable('1990-01-01'),
bornBefore: new DateTimeImmutable('2005-01-01'),
)
If this saved you time → ☕ Buy me a coffee