Rules

Validation rules help ensure data quality and security in your Laravel applications. The Basic Components package includes advanced validation rules that go beyond Laravel's built-in validators.

These rules can be used in form requests, controller validation, or directly in Livewire components.

Valid Name Rule

The ValidNameRule validates that a name contains a realistic first and last name pattern, filtering out common test data, company names, and invalid patterns.

This rule checks for:

  • At least two name parts (first and last name)
  • Valid characters including international letters, hyphens, apostrophes
  • Filters out common test names like "John Doe", "Max Mustermann"
  • Detects company suffixes like "GmbH", "LLC", "Ltd"
  • Rejects keyboard patterns and repeated characters
  • Supports international naming conventions
1use MaskowLabs\BasicComponents\Rules\ValidNameRule;
2 
3$request->validate([
4 'name' => ['required', new ValidNameRule()],
5]);

Formatting Names

ValidNameRule::formatName() normalizes a name the same way the rule does before it validates: it trims the value, collapses repeated whitespace and capitalizes every part, including both halves of double names like Jean-Luc. Name particles such as von, van, de, da or bin are lowercased, and o'connor becomes O'Connor. null and empty strings return an empty string.

The rule validates the formatted name but does not change your input. To store the formatted name, call the method in prepareForValidation() of a form request, or before $this->validate() in a Livewire component. The rule expects a string, hence the is_string() check and bail with string in the example.

1use MaskowLabs\BasicComponents\Rules\ValidNameRule;
2 
3ValidNameRule::formatName(" jOHN o'connor "); // "John O'Connor"
4ValidNameRule::formatName('ludwig VAN beethoven'); // "Ludwig van Beethoven"
5ValidNameRule::formatName('JOSÉ GARCÍA'); // "José García"
6ValidNameRule::formatName(null); // ""
7 
8// Form request: store the formatted name
9protected function prepareForValidation(): void
10{
11 if (is_string($this->input('name'))) {
12 $this->merge(['name' => ValidNameRule::formatName($this->input('name'))]);
13 }
14}
15 
16public function rules(): array
17{
18 return [
19 'name' => ['bail', 'required', 'string', new ValidNameRule()],
20 ];
21}

Valid Email Rule

The ValidEmailRule provides enhanced email validation including disposable email detection using the verifymail.io service when an API key is configured.

Features include:

  • Standard email format validation
  • Disposable/temporary email detection (requires API key)
  • Graceful fallback when external service is unavailable
  • Configurable via basic-components.verifymail_api_key

To enable disposable email detection, add your verifymail.io API key to your config:

'verifymail_api_key' => env('VERIFYMAIL_API_KEY', null),
1use MaskowLabs\BasicComponents\Rules\ValidEmailRule;
2 
3$request->validate([
4 'email' => ['required', 'email', new ValidEmailRule()],
5]);