yii2-domain-validator
Domain validator for Yii 2., (*1)
, (*2)
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., (*3)
To install, either run, (*4)
php composer.phar require kdn/yii2-domain-validator "*"
or add, (*5)
"kdn/yii2-domain-validator": "*"
to the require
section of your composer.json
file., (*6)
Usage
Model class example:, (*7)
<?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., (*8)
-
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:, (*9)
public function rules()
{
return [
['domain', 'url'],
['domain', DomainValidator::class],
];
}
-
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
., (*10)
- 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:, (*11)
./vendor/bin/phpunit
Testing using Docker
Requirements
- Docker >= 19.03.0 (install);
- Docker Compose >= 1.25.5 (install);
- Docker plugins:
Up and running
-
Provide credentials for Composer:, (*12)
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., (*13)
-
Build images for services:, (*14)
docker buildx bake --load --pull
or, (*15)
docker buildx bake --load --pull --no-cache --progress plain
see docker buildx bake --help
for details., (*16)
-
Start service in background mode:, (*17)
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
., (*18)
-
Execute tests in the running container:, (*19)
docker-compose exec 8.1 ./vendor/bin/phpunit
Alternatively you can start a shell in the running container and execute tests from it:, (*20)
docker-compose exec 8.1 sh
$ ./vendor/bin/phpunit
-
Stop and remove containers created by up
:, (*21)
docker-compose down
You may want to remove volumes along with containers:, (*22)
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)., (*23)
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., (*24)