frostbitten/flexible-url

A Laravel validation rule that accepts URLs without a scheme and normalizes them to https://

Maintainers

Package info

github.com/snoe-labs/flexible-url

pkg:composer/frostbitten/flexible-url

Statistics

Installs: 5

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-02-11 17:51 UTC

This package is auto-updated.

Last update: 2026-03-11 18:32:44 UTC


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:

  1. Prepends https:// to get https://example.com
  2. Validates the normalized URL using Laravel's built-in url rule
  3. 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