frostbitten / flexible-url
A Laravel validation rule that accepts URLs without a scheme and normalizes them to https://
Requires
- php: ^8.2
- illuminate/contracts: ^11.0 || ^12.0
- illuminate/support: ^11.0 || ^12.0
Requires (Dev)
- orchestra/testbench: ^10.0
- pestphp/pest: ^3.0 || ^4.0
README
A Laravel validation rule that accepts URLs with or without a scheme. When the scheme is missing, it normalizes the value to https:// automatically.
Users type google.com, not https://google.com. This rule handles that gracefully.
Installation
composer require frostbitten/flexible-url
Usage
Use it as a drop-in replacement for Laravel's url rule:
use Frostbitten\FlexibleUrl\FlexibleUrl; public function rules(): array { return [ 'website' => ['nullable', new FlexibleUrl], ]; }
When a user submits example.com, the rule:
- Prepends
https://to gethttps://example.com - Validates the normalized URL using Laravel's built-in
urlrule - Updates the validator data so
$request->validated()returns the normalized value
URLs that already include http:// or https:// are left untouched.
How normalization works
The normalized value is written back to the validator's internal data via an after callback. This means $validator->validated() (and by extension $request->validated() in Form Requests) returns the normalized URL.
This works in all contexts:
- Form Requests
- Livewire validation
- Manual
Validator::make()calls - Artisan commands
After-normalization callback
For cases where you need additional control, pass a callback:
new FlexibleUrl(function (string $attribute, string $normalizedValue) { // e.g. update a Livewire property $this->$attribute = $normalizedValue; })
The callback is only invoked when normalization actually occurs (i.e. a scheme was prepended). It is not called when the URL already has a scheme.
Requirements
- PHP 8.2+
- Laravel 11 or 12
License
MIT