PHP code example of jp3cki / yii2-extra-validator

1. Go to this page and download the library: Download jp3cki/yii2-extra-validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jp3cki / yii2-extra-validator example snippets


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()],
        ];
    }
}

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 です。
            ],
        ];
    }
}

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 です。
            ],
        ];
    }
}

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()],
        ];
    }
}

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();
    }
}

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 です。
            ],
        ];
    }
}


return [
    'adminEmail' => '[email protected]',
    'recaptchaSiteKey' => 'YOUR-SITE-KEY', // <- This!
    'recaptchaSecret' => 'YOUR-SECRET-KEY', // <- and this!
];


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
    }
}

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]
        ];
    }
}


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',
        ];
    }
}


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'], ' ];
    }
}