2017 © Pedro Peláez
 

yii2-extension yii2-extra-validator

Validators for Yii2

image

jp3cki/yii2-extra-validator

Validators for Yii2

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 13 Versions
  • 0 % Grown

The README.md

yii2-extra-validator

License Latest Stable Version, (*1)

Requirements

  • PHP 8.0 or later
  • Yii framework 2.0
  • Some php extensions is required:
    • PCRE extension (also required by Yii)
    • mbstring extension (also required by Yii)
    • intl extension (also required by Yii in some action)

Install

  1. Set up Composer, the de facto standard package manager.
  2. Set up your new Yii app if needed.
  3. php composer.phar require jp3cki/yii2-extra-validator

Usage

This package includes these validators: - AvailableUrlValidator - ConvertCharacterWidthFilterValidator - HiraganaValidator - IdnToPunycodeFilterValidator - KatakanaValidator - ReCaptchaValidator - StrictUrlValidator - TwitterAccountValidator - ZenginNameFilterValidator - ZenginNameValidator, (*2)

AvailableUrlValidator

AvailableUrlValidator will check that the URL is accessible., (*3)

Validation will failed if remote server returns 4xx(client error) or 5xx(server error)., (*4)

Model class example:, (*5)

namespace app\models;

use yii\base\Model;
use jp3cki\yii2\validators\AvailableUrlValidator;
// use jp3cki\yii2\validators\StrictUrlValidator;

class YourCustomForm extends Model
{
    public $url;

    public function rules()
    {
        return [
            [['url'], 'url', 'enableIDN' => true], // <- Core validator
            // [['url'], StrictUrlValidator::className(), 'enableIDN' => true]
            [['url'], AvailableUrlValidator::className()],
        ];
    }
}

ConvertCharacterWidthFilterValidator

ConvertCharacterWidthFilterValidator is a filter validator that provides normalization of character width., (*6)

This validator may useful for Japanese input., (*7)

このフィルタバリデータを利用すると、半角カタカナの入力を全角に変換したり、全角英数を半角英数に変換したり、カタカナをひらがなに変換したりできます。, (*8)

HiraganaValidatorKatakanaValidatorと組み合わせて使用すると便利かもしれません。, (*9)

Model class example:, (*10)

namespace app\models;

use yii\base\Model;
use jp3cki\yii2\validators\ConvertCharacterWidthFilterValidator;

class YourCustomForm extends Model
{
    public $value;

    public function rules()
    {
        return [
            [['value'], ConvertCharacterWidthFilterValidator::className(),
                'option' => 'asKV', // mb_convert_kana() 関数の変換書式を指定します。デフォルトは asKV です。
                'charset' => 'UTF-8', // 必要であれば文字コードを指定します。デフォルトは Yii::app()->charset で、通常 UTF-8 です。
            ],
        ];
    }
}

HiraganaValidator

HiraganaValidator validates value that input is hiragana-only string., (*11)

このバリデータは入力がひらがなのみで構成されていることを検証します。名前のふりがな入力等に利用できます。, (*12)

カタカナの検証を行いたい場合はKatakanaValidatorを使用します。, (*13)

Model class example:, (*14)

namespace app\models;

use yii\base\Model;
use jp3cki\yii2\validators\HiraganaValidator;

class YourCustomForm extends Model
{
    public $value;

    public function rules()
    {
        return [
            [['value'], HiraganaValidator::className(),
                'acceptSpace' => false,  // スペース(半角・全角)を許容する場合は true を設定します。デフォルトは false です。
                'charset' => 'UTF-8', // 必要であれば文字コードを指定します。デフォルトは Yii::app()->charset で、通常 UTF-8 です。
            ],
        ];
    }
}

IdnToPunycodeFilterValidator

IdnToPunycodeFilterValidator is a filter validator that provides convert IDN to Punycoded domain name., (*15)

This validator may useful when you store URL to the database in ASCII charset., (*16)

Model class example:, (*17)

namespace app\models;

use yii\base\Model;
use jp3cki\yii2\validators\IdnToPunycodeFilterValidator;
// use jp3cki\yii2\validators\StrictUrlValidator;

class YourCustomForm extends Model
{
    public $url;

    public function rules()
    {
        return [
            [['url'], 'url', 'enableIDN' => true], // <- Core validator
            // [['url'], StrictUrlValidator::className(), 'enableIDN' => true]
            [['url'], IdnToPunycodeFilterValidator::className()],
        ];
    }
}

Controller class example:, (*18)

public function actionUpdate()
{
    $model = new YourCustomForm();
    $model->url = 'http://ドメイン名例.JP/'; // user input
    if ($model->validate()) {
        // $model->url is now 'http://xn--eckwd4c7cu47r2wf.jp/'

        // $dbModel->url = $model->url;
        // $dbModel->save();
    }
}

KatakanaValidator

KatakanaValidator validates value that input is katakana-only string., (*19)

このバリデータは入力がカタカナのみで構成されていることを検証します。名前のフリガナ入力等に利用できます。, (*20)

ひらがなの検証を行いたい場合はHiraganaValidatorを使用します。, (*21)

Model class example:, (*22)

namespace app\models;

use yii\base\Model;
use jp3cki\yii2\validators\KatakanaValidator;

class YourCustomForm extends Model
{
    public $value;

    public function rules()
    {
        return [
            [['value'], KatakanaValidator::className(),
                'acceptSpace' => false,  // スペース(半角・全角)を許容する場合は true を設定します。デフォルトは false です。
                'charset' => 'UTF-8', // 必要であれば文字コードを指定します。デフォルトは Yii::app()->charset で、通常 UTF-8 です。
            ],
        ];
    }
}

ReCaptchaValidator

ReCaptchaValidator validates reCAPTCHA (API ver.2) input., (*23)

First, you must visit reCAPTCHA website and create your keys., (*24)

After reCAPTCHA registration, you can get Site key and Secret key., (*25)

Open @app/config/params.php file and add these keys like below:, (*26)

<?php
return [
    'adminEmail' => 'admin@example.com',
    'recaptchaSiteKey' => 'YOUR-SITE-KEY', // <- This!
    'recaptchaSecret' => 'YOUR-SECRET-KEY', // <- and this!
];

In the HTML output phase, you can use the HTML snippet in the website of reCAPTCHA., (*27)

In validation phase, you can set $_POST['g-recaptcha-response'] parameter to our validator and verification., (*28)

<?php
namespace app\models;

use Yii;
use yii\base\Model;
use jp3cki\yii2\validators\ReCaptchaValidator;

class ReCaptchaForm extends Model
{
    public $recaptcha;

    public function rules()
    {
        return [
            [['recaptcha'], ReCaptchaValidator::className(),
                'secret' => Yii::$app->params['recaptchaSecret']], // <- set SECRET KEY to the validator
        ];
    }

    public function attributeLabels()
    {
        return [
            'recaptcha' => 'reCAPTCHA',
        ];
    }
}
// (in Controller class)
public function actionUpdate()
{
    $request = Yii::$app->request;

    $form = new ReCaptchaForm();
    $form->recaptcha = $request->post('g-recaptcha-response'); // <- set g-recptcha-response to the validator
    if ($form->validate()) {
        // ok
    } else {
        // failed
    }
}

StrictUrlValidator

StrictUrlValidator validates URL that checks strictly than core validator implementation., (*29)

Input UrlValidator(core) StrictUrlValidator(this)
http://example.com/ valid valid
http://example.com/あ valid invalid
http://example.comあ/ valid invalid

Model class example:, (*30)

namespace app\models;

use yii\base\Model;
use jp3cki\yii2\validators\StrictUrlValidator;

class YourCustomForm extends Model
{
    public $url;

    public function rules()
    {
        return [
            [['url'], StrictUrlValidator::className(), 'enableIDN' => true]
        ];
    }
}

TwitterAccountValidator

TwitterAccountValidator validates Twitter's @id name(aka screen name)., (*31)

By default, our validator repels blacklisted account name like mentions., (*32)

Model class example:, (*33)

<?php
namespace app\models;

use Yii;
use yii\base\Model;
use jp3cki\yii2\validators\TwitterAccountValidator;

class YourCustomForm extends Model
{
    public $screenName;

    public function rules()
    {
        return [
            [['screenName'], TwitterAccountValidator::className()],
        ];
    }

    public function attributeLabels()
    {
        return [
            'screenName' => 'Screen Name',
        ];
    }
}

Currently, we do not support client-side(JavaScript) validation., (*34)

ZenginNameFilterValidator

ZenginNameValidator

These validators are useful if you are dealing with a bank account for the Japanese app., (*35)

ZenginNameFilterValidator は入力された文字列を(自動判断できる範囲で)全銀用の文字列に変換します。, (*36)

実際の利用では、口座名義などにフィルタをかけ、実際のバリデータである ZenginNameValidator へデータへ引き継ぐことになります。, (*37)

ZenginNameValidator は入力された文字列が全銀用の文字列として妥当か検査します。 このバリデータは例えば全角カタカナを受け付けないので、 ZenginNameFilterValidator を事前に通して入力の補助とできます。 (半角カタカナで入力を強制するのは非人道的です。また、長音の代わりにハイフンを使用する必要があるなど人間が徹底して守るにはつらいです), (*38)

Model class example:, (*39)

<?php
namespace app\models;

use Yii;
use yii\base\Model;
use jp3cki\yii2\validators\ZenginNameFilterValidator;
use jp3cki\yii2\validators\ZenginNameValidator;

class YourCustomForm extends Model
{
    public $accountName; // 銀行口座名義

    public function rules()
    {
        return [
            // [['accountName'], 'required'],
            [['accountName'], ZenginNameFilterValidator::className()],
            [['accountName'], ZenginNameValidator::className()],
            // [['accountName'], 'string', 'max' => 30],
        ];
    }

    public function attributeLabels()
    {
        return [
            'accountName' => '銀行口座名義',
        ];
    }
}

License

The MIT License., (*40)

The MIT License (MIT)

Copyright (c) 2015-2023 AIZAWA Hina <hina@fetus.jp>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Contributing

Patches and/or report issues are welcome., (*41)

  • Please create new branch for each issue or feature. (should not work in master branch)
  • Please write and run test. $ make test
  • Coding style is PSR-2.
    • Please run check-style for static code analysis and coding rule checking. $ make check-style
  • Please clean up commits.
  • Please create new pull-request for each issue or feature.
  • Please gazing the results of Travis-CI and other hooks.
  • Please use Japanese or very simple English to create new pull-request or issue.

I strongly hope rewrite my poor English., (*42)

The Versions

19/08 2015
04/06 2015
16/03 2015

1.3.0

1.3.0.0

Validators for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

extension yii2 validator twitter recaptcha

16/03 2015

1.2.0

1.2.0.0

Validators for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

extension yii2 validator twitter recaptcha

16/03 2015

dev-phone

dev-phone

Validators for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

extension yii2 validator twitter recaptcha

12/03 2015

1.1.0

1.1.0.0

Validators for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

extension yii2 validator twitter recaptcha

03/03 2015

1.0.2

1.0.2.0

Validators for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

extension yii2 validator twitter recaptcha

01/03 2015

1.0.1

1.0.1.0

Validators for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

extension yii2 validator twitter recaptcha

28/02 2015

1.0.0

1.0.0.0

Validators for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

extension yii2 validator twitter recaptcha

28/02 2015

1.0.0-alpha1

1.0.0.0-alpha1

Validators for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

extension yii2 validator twitter recaptcha