PHPackages                             tanthammar/laravel-rules - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. tanthammar/laravel-rules

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

tanthammar/laravel-rules
========================

Custom validation rules for the Laravel Framework

6.3.1(3mo ago)61.5k↓50%1MITPHPPHP ^8.1|^8.2|^8.3|^8.4

Since Jun 23Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/tanthammar/laravel-rules)[ Packagist](https://packagist.org/packages/tanthammar/laravel-rules)[ Docs](https://github.com/tanthammar/laravel-rules)[ RSS](/packages/tanthammar-laravel-rules/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (28)Used By (1)

Laravel Rules &amp; Validators
==============================

[](#laravel-rules--validators)

Custom validation rules, factory helpers, and services for Laravel applications, with a focus on Swedish localization.

Requirements
------------

[](#requirements)

- PHP 8.1+
- Laravel 10.0+

Installation
------------

[](#installation)

```
composer require tanthammar/laravel-rules
```

Validation Rules
----------------

[](#validation-rules)

### Swedish Personal &amp; Organization Numbers

[](#swedish-personal--organization-numbers)

```
use TantHammar\LaravelRules\Rules\PersonNummer;
use TantHammar\LaravelRules\Rules\OrgNummer;
use TantHammar\LaravelRules\Rules\PersonOrOrgNummer;

// Swedish personal identification numbers
$request->validate([
    'person_number' => [new PersonNummer]
]);

// Swedish organization numbers
$request->validate([
    'org_number' => [new OrgNummer]
]);

// Accept either personal or organization numbers
$request->validate([
    'number' => [new PersonOrOrgNummer]
]);
```

### Phone Numbers

[](#phone-numbers)

```
use TantHammar\LaravelRules\Rules\PhoneNumber;
use TantHammar\LaravelRules\Rules\MobileNumber;
use TantHammar\LaravelRules\Rules\FixedLineNumber;

// International phone numbers (handles missing + prefix automatically)
$request->validate([
    'phone' => [new PhoneNumber]
]);

// Mobile numbers only
$request->validate([
    'mobile' => [new MobileNumber]
]);

// Fixed line numbers only
$request->validate([
    'landline' => [new FixedLineNumber]
]);
```

### Swedish Banking

[](#swedish-banking)

```
use TantHammar\LaravelRules\Rules\BankKonto;
use TantHammar\LaravelRules\Rules\BankGiro;
use TantHammar\LaravelRules\Rules\PlusGiro;

// Swedish bank account numbers
$request->validate([
    'bank_account' => [new BankKonto]
]);

// BankGiro payment numbers
$request->validate([
    'bankgiro' => [new BankGiro]
]);

// PlusGiro payment numbers
$request->validate([
    'plusgiro' => [new PlusGiro]
]);
```

### VAT Number Validation

[](#vat-number-validation)

⚠️ **Important**: Choose the right VAT validation rule based on your needs:

#### VatNumberFormat (Recommended for most cases)

[](#vatnumberformat-recommended-for-most-cases)

Only validates the format using regex - no external API calls.

```
use TantHammar\LaravelRules\Rules\VatNumberFormat;

$request->validate([
    'vat_number' => [new VatNumberFormat]
]);
```

#### VatNumberAPI (Use with caution)

[](#vatnumberapi-use-with-caution)

Validates against the VIES EU API but throws exceptions when service is unavailable.

```
use TantHammar\LaravelRules\Rules\VatNumberAPI;
use Mpociot\VatCalculator\Exceptions\VATCheckUnavailableException;

try {
    $request->validate([
        'vat_number' => [new VatNumberAPI]
    ]);
} catch (VATCheckUnavailableException $e) {
    // Handle service unavailability
    // Service can be down for hours or days
    return back()->withErrors([
        'vat_number' => 'VAT validation service is temporarily unavailable. Please try again later.'
    ]);
}
```

#### VatNumberAPIWithFormatFallback (Balanced approach)

[](#vatnumberapiwithformatfallback-balanced-approach)

Tries API validation first, falls back to format validation if service is unavailable.

```
use TantHammar\LaravelRules\Rules\VatNumberAPIWithFormatFallback;

$request->validate([
    'vat_number' => [new VatNumberAPIWithFormatFallback]
]);
```

### Geographic Coordinates

[](#geographic-coordinates)

```
use TantHammar\LaravelRules\Rules\Latitude;
use TantHammar\LaravelRules\Rules\Longitude;

$request->validate([
    'lat' => [new Latitude],
    'lng' => [new Longitude]
]);
```

### File Upload Validation

[](#file-upload-validation)

```
use TantHammar\LaravelRules\Rules\MaxTotalFileSize;

// Limit total size of all uploaded files
$request->validate([
    'documents.*' => ['file'],
    'documents' => [new MaxTotalFileSize(5000)] // 5MB total
]);
```

Factory Helpers
---------------

[](#factory-helpers)

Generate realistic test data for Swedish formats:

```
use TantHammar\LaravelRules\Factories\FakePhoneNumber;
use TantHammar\LaravelRules\Factories\FakeMobileNumber;
use TantHammar\LaravelRules\Factories\FakeBankgiro;
use TantHammar\LaravelRules\Factories\FakePlusgiro;

// Generate Swedish phone numbers
$phone = FakePhoneNumber::make(international: true);
$localPhone = FakePhoneNumber::make(international: false);

// GDPR-safe phone numbers (official test ranges)
$gdprPhone = FakePhoneNumber::gdprSafe();

// Mobile numbers
$mobile = FakeMobileNumber::make();

// Payment numbers
$bankgiro = FakeBankgiro::make();
$plusgiro = FakePlusgiro::make();
```

Services
--------

[](#services)

### VAT Information Lookup

[](#vat-information-lookup)

#### Get Business Name Only

[](#get-business-name-only)

```
use TantHammar\LaravelRules\Services\BusinessNameFromVatID;
use TantHammar\LaravelRules\Enums\BusinessNameLookupError;

// Get business name from VAT ID
$result = BusinessNameFromVatID::lookup('SE556556567801');

if ($result instanceof BusinessNameLookupError) {
    // Handle error cases
    match ($result) {
        BusinessNameLookupError::Unknown => 'VAT ID not found',
        BusinessNameLookupError::ServiceUnavailable => 'VAT service temporarily unavailable',
        BusinessNameLookupError::Invalid => 'Invalid VAT ID format',
    };

    // Or use the built-in label method
    $errorMessage = $result->label();
} else {
    // Success - $result is the business name string
    $businessName = $result;
}
```

#### Get Full VAT Details Object

[](#get-full-vat-details-object)

```
use TantHammar\LaravelRules\Services\VatDetailsFromVatID;

// Get full VAT details object
$details = VatDetailsFromVatID::lookup('SE556556567801');

// Check if lookup was successful
if ($details->valid) {
    // Success - access object properties
    $countryCode = $details->countryCode;
    $vatNumber = $details->vatNumber;
    $requestDate = $details->requestDate;
    $businessName = $details->name;
    $businessAddress = $details->address;
} else {
    // Failed - returns empty object with valid = false
    // This happens when VAT ID is invalid or service is unavailable
    $errorMessage = 'VAT ID not found or service unavailable';
}
```

### Business Type Detection

[](#business-type-detection)

```
use TantHammar\LaravelRules\Helpers\BusinessTypeFromNr;

// Determine if number is personal, business, or undefined
$type = BusinessTypeFromNr::make('199001011234'); // Returns: 'individual'
$type = BusinessTypeFromNr::make('556556567801'); // Returns: 'business'
$type = BusinessTypeFromNr::make('invalid');     // Returns: 'undefined'
```

Legacy Helper Methods (Deprecated)
----------------------------------

[](#legacy-helper-methods-deprecated)

These methods are deprecated but still available for backwards compatibility:

```
use TantHammar\LaravelRules\RuleHelpers;

// Use BusinessNameFromVatID::lookup() instead
$name = RuleHelpers::getBusinessNameFromVatID($vatId);

// Use VatDetailsFromVatID::lookup() instead
$details = RuleHelpers::getVATDetailsFromVatID($vatId);

// Use BusinessTypeFromNr::make() instead
$type = RuleHelpers::check_business_type($number);
```

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance86

Actively maintained with recent releases

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~50 days

Total

27

Last Release

102d ago

Major Versions

1.1.0 → 2.0.02022-06-27

2.6.2 → 3.0.02023-04-12

3.4.1 → 4.0.02025-07-03

4.0.0 → 5.0.02025-07-03

5.0.0 → 6.0.02025-07-03

PHP version history (4 changes)1.0.0PHP ^8.1

2.6.2PHP ^8.1|^8.2

3.2.0PHP ^8.1|^8.2|^8.3

3.4.0PHP ^8.1|^8.2|^8.3|^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/e71fd6e89c09af4ce7dc4a1c4fd3e04716326b6e98ed9003f092a8462a5ef422?d=identicon)[Stockholm](/maintainers/Stockholm)

---

Top Contributors

[![tanthammar](https://avatars.githubusercontent.com/u/21239634?v=4)](https://github.com/tanthammar "tanthammar (53 commits)")

---

Tags

laravelvalidationrulestanthammar

### Embed Badge

![Health badge](/badges/tanthammar-laravel-rules/health.svg)

```
[![Health](https://phpackages.com/badges/tanthammar-laravel-rules/health.svg)](https://phpackages.com/packages/tanthammar-laravel-rules)
```

###  Alternatives

[resultsystems/validation

Inspired 'KennedyTedesco Validation' - The power of 'Respect Validation' on Laravel.

2832.4k4](/packages/resultsystems-validation)[carsdotcom/laravel-json-schema

Json Schema validation for Laravel projects

1036.7k3](/packages/carsdotcom-laravel-json-schema)[iutrace/laravel-cuit-validator

Argentinian CUIT and CUIL Validator

1013.3k](/packages/iutrace-laravel-cuit-validator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
