kdn / yii2-domain-validator
Domain name validator for Yii 2.
Installs: 104 848
Dependents: 2
Suggesters: 0
Security: 0
Stars: 11
Watchers: 2
Forks: 1
Open Issues: 3
Type:yii2-extension
Requires
- php: >=5.4.0
- ext-ctype: *
- ext-mbstring: *
- yiisoft/yii2: 2.*
Requires (Dev)
- phpunit/php-invoker: >=1.0 <4.0
- phpunit/phpunit: >=4.0 <10.0
Suggests
- ext-intl: this extension is needed for IDN support and to run all tests, otherwise some tests will be skipped
- ext-runkit: this extension is needed to run all tests, otherwise some tests will be skipped
README
Domain validator for Yii 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.
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.
-
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 beEach label of the input value can consist of only letters, numbers and hyphens
, although it would be better to show something likeInvalid 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 wanthttp://example.com
orhttp.example.com
. If you don't need URLs at all, only stand-alone domain name, you can disable this behavior by settingallowURL
tofalse
. 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], ]; }
-
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 nameexample
is valid. I added this restriction for practical reasons, you can disable it or require even more domain name labels using optionlabelNumberMin
. -
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
-
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 ofauth.json
on{}
, in most cases this will work. -
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. -
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
and5.6-alpine
, see services defined indocker-compose.yml
. -
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
-
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.