Installation
composer require intervention/validation
First Use Case Validate a credit card number in a form request:
use Intervention\Validation\Rules\Creditcard;
$validator = Validator::make($request->all(), [
'card_number' => ['required', new Creditcard],
]);
Where to Look First
vendor/intervention/validation/src/Rules/ for rule-specific examples (e.g., Iban.php, Isbn.php).Inline Rule Usage
// In a FormRequest or Controller
$validator = Validator::make($data, [
'iban' => ['required', new \Intervention\Validation\Rules\Iban],
'isbn' => ['nullable', new \Intervention\Validation\Rules\Isbn],
]);
Customizing Rules
new \Intervention\Validation\Rules\Iban('DE') // Validate German IBANs
$validator->setCustomMessages([
'iban' => [
'iban' => 'The :attribute must be a valid IBAN for Germany.',
],
]);
Form Request Integration
use Intervention\Validation\Rules\Hexadecimalcolor;
public function rules()
{
return [
'hex_color' => ['sometimes', new Hexadecimalcolor],
];
}
Batch Validation
$validator = Validator::makeEach($request->all(), [
'*.iban' => new \Intervention\Validation\Rules\Iban,
'*.credit_card' => new \Intervention\Validation\Rules\Creditcard,
]);
Dynamic Rule Application
$rules = ['email' => 'required|email'];
if ($request->has('is_admin')) {
$rules['iban'] = new \Intervention\Validation\Rules\Iban;
}
Validator::make($request->all(), $rules);
ValidationTestCase or mock Validator facade:
$validator = Validator::make(['iban' => 'DE89370400440532013000'], [
'iban' => new \Intervention\Validation\Rules\Iban,
]);
$this->assertTrue($validator->passes());
ValidatedData or FailedValidationException:
try {
$validated = $request->validate([
'isbn' => new \Intervention\Validation\Rules\Isbn,
]);
} catch (\Illuminate\Validation\ValidationException $e) {
return response()->json($e->errors(), 422);
}
Rule Naming Collisions
hexadecimalcolor vs. hex_color). Use fully qualified class names:
new \Intervention\Validation\Rules\Hexadecimalcolor // Explicit
Locale-Specific Rules
Iban, Phone) require country/region parameters. Omitting them may lead to silent failures or broad validations:
// ❌ Ambiguous (may validate any country)
new \Intervention\Validation\Rules\Iban
// ✅ Explicit
new \Intervention\Validation\Rules\Iban('US')
Performance with Complex Rules
Creditcard or Isbn may involve regex or external checks. Test under load if validating thousands of records.Deprecated Laravel Versions
// Override all messages for a rule
$validator->setCustomMessages([
'iban.iban' => 'Invalid IBAN format.',
]);
vendor/intervention/validation/src/Rules/Creditcard.php) for edge cases or custom logic.validating or failed events to log or transform data:
Validator::extend('iban', function ($attribute, $value, $parameters) {
// Custom logic
return true;
});
Custom Rules
Extend the package by creating a new rule class (e.g., App\Rules\CustomValidation):
use Intervention\Validation\Rules\AbstractRule;
class CustomValidation extends AbstractRule {
public function passes($attribute, $value) {
return str_contains($value, 'custom');
}
}
Register it in AppServiceProvider:
Validator::extend('custom', 'App\Rules\CustomValidation');
Rule Parameters
Modify how parameters are parsed in rules (e.g., for Phone validation):
new \Intervention\Validation\Rules\Phone('US', 'mobile')
Testing
$this->partialMock(\Intervention\Validation\Rules\Creditcard::class, function ($mock) {
$mock->shouldReceive('passes')->andReturn(true);
});
ValidationTestCase for integration tests.Configuration
resources/lang/.new \Intervention\Validation\Rules\Iban | 'max:34'
/**
* @OA\Parameter(
* name="iban",
* in="query",
* required=true,
* @OA\Schema(type="string", format="iban")
* )
*/
// VeeValidate example
this.$validator.extend('iban', {
validate: value => /^[A-Z]{2}\d{2}[A-Z0-9]{11,30}$/i.test(value),
message: 'Invalid IBAN format.'
});
How can I help you explore Laravel packages today?