PHPackages                             horat1us/yii2-url-validator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. horat1us/yii2-url-validator

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

horat1us/yii2-url-validator
===========================

Yii2 UrlValidator with single-label host support (http://localhost:8000)

1.0.0(1mo ago)00MITPHPPHP &gt;=8.1

Since Jul 8Pushed 1mo agoCompare

[ Source](https://github.com/Horat1us/yii2-url-validator)[ Packagist](https://packagist.org/packages/horat1us/yii2-url-validator)[ RSS](/packages/horat1us-yii2-url-validator/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (4)Versions (2)Used By (0)

Yii2 Url Validator
==================

[](#yii2-url-validator)

[![Latest Stable Version](https://camo.githubusercontent.com/66bc593926c32a114397ab60af1e092c3adac2afcac59d4386c2ef04e6c4ef29/68747470733a2f2f706f7365722e707567782e6f72672f686f7261743175732f796969322d75726c2d76616c696461746f722f762f737461626c65)](https://packagist.org/packages/horat1us/yii2-url-validator)[![Total Downloads](https://camo.githubusercontent.com/276b8d2b6b1ae56ae111e5c108a65dada5925c0e8ee9777c3cd500b31911e3b2/68747470733a2f2f706f7365722e707567782e6f72672f686f7261743175732f796969322d75726c2d76616c696461746f722f646f776e6c6f616473)](https://packagist.org/packages/horat1us/yii2-url-validator)[![License](https://camo.githubusercontent.com/d931f42e410475a08919dd6d315a34997eb503b42de1e5d5cb08da215177e8a7/68747470733a2f2f706f7365722e707567782e6f72672f686f7261743175732f796969322d75726c2d76616c696461746f722f6c6963656e7365)](https://packagist.org/packages/horat1us/yii2-url-validator)[![PHP Tests](https://github.com/Horat1us/yii2-url-validator/actions/workflows/php.yml/badge.svg)](https://github.com/Horat1us/yii2-url-validator/actions/workflows/php.yml)

Drop-in replacement for Yii2 `UrlValidator` that can accept single-label hosts such as `http://localhost:8000/`, which the stock validator rejects.

Installation
------------

[](#installation)

```
composer require horat1us/yii2-url-validator
```

Usage
-----

[](#usage)

```
use Horat1us\Yii\UrlValidator;
use yii\base;

class YourModel extends base\Model
{
    public ?string $url = null;

    public function rules(): array
    {
        return [
            ["url", "required",],
            ["url", UrlValidator::class, "allowSingleLabelHost" => true,],
        ];
    }
}
```

With `allowSingleLabelHost` enabled:

URLStock Yii2This package`http://example.com/`validvalid`http://127.0.0.1:8080/`validvalid`http://localhost`invalidvalid`http://localhost:8000/path?q=1#f`invalidvalid`http://my-host:3000/`invalidvalid`http://user@localhost/`invalidinvalid`http://[::1]/`invalidinvalid`http://localhost:999999/`invalidinvalidLeaving `allowSingleLabelHost` at its default (`false`) reproduces stock Yii2 behaviour exactly.

The option relaxes only the host part of the pattern: a hostname may consist of a single label. It does **not** enable userinfo (`http://user@localhost/`), IPv6 literals (`http://[::1]/`), out-of-range ports (`http://localhost:999999/`), or empty hosts (`http://`, `http:///`).

### Custom patterns

[](#custom-patterns)

`allowSingleLabelHost` only relaxes the host part of Yii's *default* pattern. If you supply your own `pattern`, it is used verbatim and the flag has no effect:

```
["url", UrlValidator::class, "allowSingleLabelHost" => true, "pattern" => '/^{schemes}:\/\/internal$/i',],
```

### Client-side validation

[](#client-side-validation)

`allowSingleLabelHost` is applied in `init()` to the `pattern` property, so `getClientOptions()`and `clientValidateAttribute()` emit the relaxed regular expression too. Server-side and client-side validation stay in sync with no extra configuration.

Features
--------

[](#features)

- Extends `yii\validators\UrlValidator`; all inherited options (`validSchemes`, `defaultScheme`, `enableIDN`, `message`, `skipOnEmpty`, ...) keep working
- Permits any single-label host, not just the literal `localhost`
- Never silently overwrites a user-supplied `pattern`
- Client-side validation supported out of the box
- Throws `InvalidConfigException` rather than silently degrading if a future Yii2 release changes its default pattern
- Fully tested

Contributing
------------

[](#contributing)

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance90

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

49d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2751367?v=4)[Horat1us](/maintainers/Horat1us)[@Horat1us](https://github.com/Horat1us)

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/horat1us-yii2-url-validator/health.svg)

```
[![Health](https://phpackages.com/badges/horat1us-yii2-url-validator/health.svg)](https://phpackages.com/packages/horat1us-yii2-url-validator)
```

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.7M3.5k](/packages/craftcms-cms)[skeeks/cms

SkeekS CMS — control panel and tools based on php framework Yii2

13926.0k66](/packages/skeeks-cms)[borales/yii2-phone-input

Yii2 International telephone numbers - Asset Bundle, Behavior, Validator, Widget

1331.7M6](/packages/borales-yii2-phone-input)[codeonyii/yii2-at-least-validator

Validates at least one (or more) attributes.

28266.9k1](/packages/codeonyii-yii2-at-least-validator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
