kdn/yii2-domain-validator

Domain name validator for Yii 2.

Installs: 105 827

Dependents: 2

Suggesters: 0

Security: 0

Stars: 11

Watchers: 2

Forks: 1

Open Issues: 3

Type:yii2-extension

1.1.4 2022-06-11 08:53 UTC

This package is auto-updated.

Last update: 2024-10-30 01:31:43 UTC


README

Domain validator for Yii 2.

License Latest Stable Version Code Coverage Scrutinizer Code Quality Code Climate

Requirements

  • PHP 5.4 or later or HHVM 3;
  • Yii framework 2;
  • PHP extensions:
    • ctype (character type checking) extension (required);
    • mbstring (multibyte string) extension (required);
    • intl (internationalization functions) extension (optional, for IDN only).

Installation

The preferred way to install this extension is through Composer.

To install, either run

php composer.phar require kdn/yii2-domain-validator "*"

or add

"kdn/yii2-domain-validator": "*"

to the require section of your composer.json file.

Usage

Model class example:

<?php

namespace app\models;

use kdn\yii2\validators\DomainValidator;
use Yii;
use yii\base\Model;

class YourCustomModel extends Model
{
    public $domain;

    public function rules()
    {
        return [
            ['domain', DomainValidator::class],
            /*
            or with custom options: enable IDN and forbid URLs
            [
                'domain',
                DomainValidator::class,
                'enableIDN' => true,
                'allowURL' => false,
            ],
            */
        ];
    }

    public function attributeLabels()
    {
        return [
            'domain' => Yii::t('app', 'Domain Name'),
        ];
    }
}

Please view public properties in class DomainValidator to get info about all available options, they documented comprehensively. Here I will highlight only non-evident things.

  1. By default, validator allows URL, it will try to parse URL and then validate domain name. Note that model attribute value itself will not be modified. If URL parsing fails then validator considers value as domain. Validator may work not perfect for invalid URLs. For example user input is http//example.com, the error message will be Each label of the input value can consist of only letters, numbers and hyphens, although it would be better to show something like Invalid URL. The problem is that if field allows both URL and bare domain name and the input value is invalid, then it is impossible to reliably determine what did user want http://example.com or http.example.com. If you don't need URLs at all, only stand-alone domain name, you can disable this behavior by setting allowURL to false. If you always need to validate domain name in URL, no stand-alone domain name, then you should add URL validator before domain name validator:

    public function rules()
    {
        return [
            ['domain', 'url'],
            ['domain', DomainValidator::class],
        ];
    }
  2. By default, minimum number of domain name labels is 2. So example - invalid, example.com - valid. It is not standard requirement for domain name, standard states that domain name example is valid. I added this restriction for practical reasons, you can disable it or require even more domain name labels using option labelNumberMin.

  3. Client side validation not implemented, and I have not such plans. Please consider AJAX validation if you want to bring domain validation on client side.

Testing

Make sure you installed all Composer dependencies (run composer update in the base directory of repository). Run PHPUnit in the base directory of repository:

./vendor/bin/phpunit

Testing using Docker

Requirements

Up and running

  1. Provide credentials for Composer:

    cp auth.json.example \
        auth.json

    I suggest to set GitHub OAuth token (also known as personal access token) in auth.json, however if you have doubts about security, or you are lazy to generate token then you can replace content of auth.json on {}, in most cases this will work.

  2. Build images for services:

    docker buildx bake --load --pull

    or

    docker buildx bake --load --pull --no-cache --progress plain

    see docker buildx bake --help for details.

  3. Start service in background mode:

    docker-compose up --detach 8.1

    This command will start the service with PHP 8.1. Also allowed 7.4, 5.6, 8.1-alpine, 7.4-alpine and 5.6-alpine, see services defined in docker-compose.yml.

  4. Execute tests in the running container:

    docker-compose exec 8.1 ./vendor/bin/phpunit

    Alternatively you can start a shell in the running container and execute tests from it:

    docker-compose exec 8.1 sh
    $ ./vendor/bin/phpunit
  5. Stop and remove containers created by up:

    docker-compose down

    You may want to remove volumes along with containers:

    docker-compose down --volumes

Backward compatibility promise

yii2-domain-validator is using Semver. This means that versions are tagged with MAJOR.MINOR.PATCH. Only a new major version will be allowed to break backward compatibility (BC).

PHP 8 introduced named arguments, which increased the cost and reduces flexibility for package maintainers. The names of the arguments for methods in yii2-domain-validator is not included in our BC promise.