|
1 |
| -# Missing validation rules and fakers, for Laravel |
2 |
| -In this repository I will add validators and factory helpers, that I use in my SaaS app. I think especially Swedish developers will find them handy. |
| 1 | +# Laravel Rules & Validators |
| 2 | +Custom validation rules, factory helpers, and services for Laravel applications, with a focus on Swedish localization. |
3 | 3 |
|
4 | 4 | ## Requirements
|
5 |
| -- PHP 8.1 |
6 |
| -- Laravel v9.0 |
| 5 | +- PHP 8.1+ |
| 6 | +- Laravel 10.0+ |
7 | 7 |
|
8 | 8 | ## Installation
|
9 | 9 | ```bash
|
10 | 10 | composer require tanthammar/laravel-rules
|
11 | 11 | ```
|
12 | 12 |
|
13 |
| -## Rules, Fakers and more |
14 |
| -See src folder |
| 13 | +## Validation Rules |
15 | 14 |
|
16 |
| -## Documentation |
17 |
| -There won't be much documentation written, this repository will grow as I add items. |
18 |
| -Hopefully the source code contains enough hints to use the components. |
| 15 | +### Swedish Personal & Organization Numbers |
| 16 | +```php |
| 17 | +use TantHammar\LaravelRules\Rules\PersonNummer; |
| 18 | +use TantHammar\LaravelRules\Rules\OrgNummer; |
| 19 | +use TantHammar\LaravelRules\Rules\PersonOrOrgNummer; |
| 20 | + |
| 21 | +// Swedish personal identification numbers |
| 22 | +$request->validate([ |
| 23 | + 'person_number' => [new PersonNummer] |
| 24 | +]); |
| 25 | + |
| 26 | +// Swedish organization numbers |
| 27 | +$request->validate([ |
| 28 | + 'org_number' => [new OrgNummer] |
| 29 | +]); |
19 | 30 |
|
20 |
| -They are all used in the same way, following Laravel conventions. |
| 31 | +// Accept either personal or organization numbers |
| 32 | +$request->validate([ |
| 33 | + 'number' => [new PersonOrOrgNummer] |
| 34 | +]); |
| 35 | +``` |
21 | 36 |
|
22 |
| -Example: |
| 37 | +### Phone Numbers |
23 | 38 | ```php
|
24 | 39 | use TantHammar\LaravelRules\Rules\PhoneNumber;
|
25 |
| -use TantHammar\LaravelRules\Rules\PersonNummer; |
| 40 | +use TantHammar\LaravelRules\Rules\MobileNumber; |
| 41 | +use TantHammar\LaravelRules\Rules\FixedLineNumber; |
| 42 | + |
| 43 | +// International phone numbers (handles missing + prefix automatically) |
| 44 | +$request->validate([ |
| 45 | + 'phone' => [new PhoneNumber] |
| 46 | +]); |
| 47 | + |
| 48 | +// Mobile numbers only |
| 49 | +$request->validate([ |
| 50 | + 'mobile' => [new MobileNumber] |
| 51 | +]); |
| 52 | + |
| 53 | +// Fixed line numbers only |
| 54 | +$request->validate([ |
| 55 | + 'landline' => [new FixedLineNumber] |
| 56 | +]); |
| 57 | +``` |
| 58 | + |
| 59 | +### Swedish Banking |
| 60 | +```php |
| 61 | +use TantHammar\LaravelRules\Rules\BankKonto; |
| 62 | +use TantHammar\LaravelRules\Rules\BankGiro; |
| 63 | +use TantHammar\LaravelRules\Rules\PlusGiro; |
| 64 | + |
| 65 | +// Swedish bank account numbers |
| 66 | +$request->validate([ |
| 67 | + 'bank_account' => [new BankKonto] |
| 68 | +]); |
| 69 | + |
| 70 | +// BankGiro payment numbers |
| 71 | +$request->validate([ |
| 72 | + 'bankgiro' => [new BankGiro] |
| 73 | +]); |
| 74 | + |
| 75 | +// PlusGiro payment numbers |
| 76 | +$request->validate([ |
| 77 | + 'plusgiro' => [new PlusGiro] |
| 78 | +]); |
| 79 | +``` |
| 80 | + |
| 81 | +### VAT Number Validation |
| 82 | + |
| 83 | +⚠️ **Important**: Choose the right VAT validation rule based on your needs: |
| 84 | + |
| 85 | +#### VatNumberFormat (Recommended for most cases) |
| 86 | +Only validates the format using regex - no external API calls. |
| 87 | +```php |
| 88 | +use TantHammar\LaravelRules\Rules\VatNumberFormat; |
| 89 | + |
| 90 | +$request->validate([ |
| 91 | + 'vat_number' => [new VatNumberFormat] |
| 92 | +]); |
| 93 | +``` |
| 94 | + |
| 95 | +#### VatNumberAPI (Use with caution) |
| 96 | +Validates against the VIES EU API but throws exceptions when service is unavailable. |
| 97 | +```php |
| 98 | +use TantHammar\LaravelRules\Rules\VatNumberAPI; |
| 99 | +use Mpociot\VatCalculator\Exceptions\VATCheckUnavailableException; |
| 100 | + |
| 101 | +try { |
| 102 | + $request->validate([ |
| 103 | + 'vat_number' => [new VatNumberAPI] |
| 104 | + ]); |
| 105 | +} catch (VATCheckUnavailableException $e) { |
| 106 | + // Handle service unavailability |
| 107 | + // Service can be down for hours or days |
| 108 | + return back()->withErrors([ |
| 109 | + 'vat_number' => 'VAT validation service is temporarily unavailable. Please try again later.' |
| 110 | + ]); |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +#### VatNumberAPIWithFormatFallback (Balanced approach) |
| 115 | +Tries API validation first, falls back to format validation if service is unavailable. |
| 116 | +```php |
| 117 | +use TantHammar\LaravelRules\Rules\VatNumberAPIWithFormatFallback; |
| 118 | + |
| 119 | +$request->validate([ |
| 120 | + 'vat_number' => [new VatNumberAPIWithFormatFallback] |
| 121 | +]); |
| 122 | +``` |
| 123 | + |
| 124 | +### Geographic Coordinates |
| 125 | +```php |
| 126 | +use TantHammar\LaravelRules\Rules\Latitude; |
| 127 | +use TantHammar\LaravelRules\Rules\Longitude; |
| 128 | + |
| 129 | +$request->validate([ |
| 130 | + 'lat' => [new Latitude], |
| 131 | + 'lng' => [new Longitude] |
| 132 | +]); |
| 133 | +``` |
| 134 | + |
| 135 | +### File Upload Validation |
| 136 | +```php |
| 137 | +use TantHammar\LaravelRules\Rules\MaxTotalFileSize; |
| 138 | + |
| 139 | +// Limit total size of all uploaded files |
| 140 | +$request->validate([ |
| 141 | + 'documents.*' => ['file'], |
| 142 | + 'documents' => [new MaxTotalFileSize(5000)] // 5MB total |
| 143 | +]); |
| 144 | +``` |
| 145 | + |
| 146 | +## Factory Helpers |
| 147 | + |
| 148 | +Generate realistic test data for Swedish formats: |
| 149 | + |
| 150 | +```php |
| 151 | +use TantHammar\LaravelRules\Factories\FakePhoneNumber; |
| 152 | +use TantHammar\LaravelRules\Factories\FakeMobileNumber; |
| 153 | +use TantHammar\LaravelRules\Factories\FakeBankgiro; |
| 154 | +use TantHammar\LaravelRules\Factories\FakePlusgiro; |
| 155 | + |
| 156 | +// Generate Swedish phone numbers |
| 157 | +$phone = FakePhoneNumber::make(international: true); |
| 158 | +$localPhone = FakePhoneNumber::make(international: false); |
| 159 | + |
| 160 | +// GDPR-safe phone numbers (official test ranges) |
| 161 | +$gdprPhone = FakePhoneNumber::gdprSafe(); |
| 162 | + |
| 163 | +// Mobile numbers |
| 164 | +$mobile = FakeMobileNumber::make(); |
| 165 | + |
| 166 | +// Payment numbers |
| 167 | +$bankgiro = FakeBankgiro::make(); |
| 168 | +$plusgiro = FakePlusgiro::make(); |
| 169 | +``` |
| 170 | + |
| 171 | +## Services |
| 172 | + |
| 173 | +### VAT Information Lookup |
| 174 | +```php |
| 175 | +use TantHammar\LaravelRules\Services\BusinessNameFromVatID; |
| 176 | +use TantHammar\LaravelRules\Services\VatDetailsFromVatID; |
| 177 | + |
| 178 | +// Get business name from VAT ID |
| 179 | +$businessName = BusinessNameFromVatID::lookup('SE556556567801'); |
| 180 | + |
| 181 | +// Get full VAT details |
| 182 | +$details = VatDetailsFromVatID::lookup('SE556556567801'); |
| 183 | +``` |
| 184 | + |
| 185 | +### Business Type Detection |
| 186 | +```php |
| 187 | +use TantHammar\LaravelRules\Helpers\BusinessTypeFromNr; |
| 188 | + |
| 189 | +// Determine if number is personal, business, or undefined |
| 190 | +$type = BusinessTypeFromNr::make('199001011234'); // Returns: 'individual' |
| 191 | +$type = BusinessTypeFromNr::make('556556567801'); // Returns: 'business' |
| 192 | +$type = BusinessTypeFromNr::make('invalid'); // Returns: 'undefined' |
| 193 | +``` |
| 194 | + |
| 195 | +## Legacy Helper Methods (Deprecated) |
| 196 | + |
| 197 | +These methods are deprecated but still available for backwards compatibility: |
| 198 | + |
| 199 | +```php |
| 200 | +use TantHammar\LaravelRules\RuleHelpers; |
| 201 | + |
| 202 | +// Use BusinessNameFromVatID::lookup() instead |
| 203 | +$name = RuleHelpers::getBusinessNameFromVatID($vatId); |
| 204 | + |
| 205 | +// Use VatDetailsFromVatID::lookup() instead |
| 206 | +$details = RuleHelpers::getVATDetailsFromVatID($vatId); |
26 | 207 |
|
27 |
| -SomeField::make('phone') |
28 |
| - ->rules([ new PhoneNumber ]) |
29 |
| -SomeField::make('person_nummer') |
30 |
| - ->rules([ new PersonNummer ]) |
| 208 | +// Use BusinessTypeFromNr::make() instead |
| 209 | +$type = RuleHelpers::check_business_type($number); |
31 | 210 | ```
|
32 | 211 |
|
33 | 212 |
|
|
0 commit comments