## Getting Started
### Minimal Steps
1. **Installation** (unchanged):
```bash
composer require intervention/validation
The package auto-registers via Laravel’s service provider discovery (no manual config needed).
First Use Case (updated with new rules): Validate a VIN number or ISO country code in a form request:
use Intervention\Validation\Rules\{Vin, CountryCode, CurrencyCode};
$validator = Validator::make($request->all(), [
'vin' => ['required', new Vin],
'country' => ['required', new CountryCode],
'currency' => ['required', new CurrencyCode],
]);
Where to Look First (updated):
Vin, CountryCode, CurrencyCode, LanguageCode, IetfLanguageTag).resources/lang/{locale}/validation.php (e.g., 'country_code' => 'Invalid ISO-3166 country code.').@method bool passes(string $attribute, string $value)).New Rule Application:
new Vin).
'vehicle_vin' => ['required', new Vin],
'country' => ['required', new CountryCode],
'language' => ['required', new LanguageCode],
'currency' => ['required', new CurrencyCode],
'locale' => ['required', new IetfLanguageTag], // e.g., "en-US"
'country' => 'required|country_code|max:2',
Form Request Integration (updated example):
use Intervention\Validation\Rules\{Vin, CountryCode, IetfLanguageTag};
public function rules()
{
return [
'vin' => ['required', new Vin],
'country' => ['required', new CountryCode],
'user_locale' => ['required', new IetfLanguageTag],
'slug' => 'required|slug',
];
}
public function messages()
{
return [
'vin.vin' => 'Invalid VIN format.',
'country.country_code' => 'Enter a valid 2-letter country code (e.g., US, DE).',
'user_locale.ietf_language_tag' => 'Invalid language tag (e.g., en-US, fr-CA).',
];
}
Dynamic Validation (updated):
$validator->sometimes('country', 'country_code', function ($input) {
return $input->has('international_shipping');
});
app()->getLocale() for dynamic validation:
$validator->after(function ($validator) {
$validator->sometimes('currency', 'currency_code', fn () => app()->getLocale() === 'en');
});
Testing (updated):
$validator = Validator::make(['vin' => '1HGCM82633A123456'], ['vin' => new Vin]);
$this->assertTrue($validator->passes());
$validator = Validator::make(['country' => 'XYZ'], ['country' => new CountryCode]);
$this->assertFalse($validator->passes());
CountryCode for geolocation APIs).country_code, currency_code).class StrictCountryCode extends CountryCode {
public function passes($attribute, $value) {
return parent::passes($attribute, strtoupper($value));
}
}
Case Sensitivity (expanded):
CountryCode/CurrencyCode are case-insensitive by default but may vary (e.g., Vin is case-sensitive).$request->merge(['country' => strtoupper($request->country)]);
VIN Validation Quirks:
sometimes() for optional fields:
'partial_vin' => ['sometimes', new Vin(['allow_partial' => true])],
IETF Language Tag Complexity:
en-US, fr-CA, or zh-Hans-CN. Overly complex tags (e.g., en-US-u-ca-islamic) may fail.preg_match('/^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$/', $value) to validate manually.Performance:
Vin involve regex checks. Cache results for repeated validations:
$vinCache = Cache::remember("vin_{$request->vin}", now()->addHours(1), fn () => new Vin);
Deprecation (unchanged):
dd($validator->errors()->messages());
// Output: ['vin' => ['Invalid VIN format.']]
$pattern = (new Vin)->getRegex();
var_dump(preg_match($pattern, '1HGCM82633A123456')); // bool(true)
Custom Rule Parameters:
CountryCode to allow specific regions:
class RegionCountryCode extends CountryCode {
public function __construct(array $allowedRegions = ['EU', 'NA']) {
$this->allowedRegions = $allowedRegions;
}
public function passes($attribute, $value) {
$isValidCountry = parent::passes($attribute, $value);
$country = $this->getCountryCode($value);
return $isValidCountry && in_array($this->getRegion($country), $this->allowedRegions);
}
}
Parameterized VIN Validation:
class FlexibleVin extends Vin {
protected $allowPartial = false;
public function __construct(bool $allowPartial = false) {
$this->allowPartial = $allowPartial;
}
public function passes($attribute, $value) {
if ($this->allowPartial && strlen($value) < 17) {
return $this->validatePartial($value);
}
return parent::passes($attribute, $value);
}
}
Testing Extensions:
public function testFlexibleVin()
{
$validator = Validator::make(['vin' => '1HGCM8'], ['vin' => new FlexibleVin(true)]);
$this->assertTrue($validator->passes());
}
CountryCode for user imports):
$users = User::whereNull('country')->get();
foreach ($users as $user) {
$validator = Validator::make(['country' => $user->country], ['country' => new CountryCode]);
if ($validator->fails()) {
$user->update(['country' => null]);
}
}
validation.es.php):
'country_code' => 'El código de país debe ser válido (ej. ES, US).',
'vin' => 'El VIN debe tener 17 caracteres.',
/**
* Validate a VIN (Vehicle Identification Number).
*
* @param string $attribute
* @param string $value
* @return bool
* @throws \InvalidArgumentException
*/
How can I help you explore Laravel packages today?