kartik-v/yii2-validators

增强的 Yii2 模型验证组件/实用工具,适用于 Yii2 框架

安装数: 79,269

依赖者: 0

建议者: 0

安全: 0

星标: 20

关注者: 5

分支: 5

开放问题: 0

类型:yii2-extension

v1.0.3 2019-08-22 12:32 UTC

This package is auto-updated.

Last update: 2024-08-29 04:09:16 UTC


README

Stable Version Unstable Version License Total Downloads Monthly Downloads Daily Downloads

此扩展为 Yii2 框架添加了新的模型验证组件,或增强了现有的 Yii2 模型验证器。

EmailValidator 扩展了 yii\validators\EmailValidator 组件,以支持 Yii2 框架的多个电子邮件输入。此外,此验证器允许设置 multiple 属性以及允许的电子邮件地址的最小和最大数量。这对于在模型中添加验证规则以及通过 JavaScript 在运行时添加对 ActiveForm 输入的增强客户端验证支持非常有用。

PhoneValidator 通过使用基于 Google 的 libphonenumber 库的 libphonenumber-for-php 扩展了 yii\validators\Validator 组件。

CardValidator 扩展了 yii\validators\Validator 组件。它使用 Luhn 校验和验证标准借记卡和信用卡号码输入。它还帮助自动检测卡片类型,并验证输入的持卡人姓名、到期日期和 CVV。

JsonValidator 扩展了 yii\validators\Validator 组件。它在文本区域字段中验证 JSON 输入。验证器还帮助通过 prettify 设置将 JSON 输入作为美化格式重写。它包括服务器端和客户端验证。

演示

您可以在 使用说明和演示 中看到此扩展的详细信息。

安装

安装此扩展的首选方式是通过 composer

注意:请检查此扩展的 composer.json 文件以获取此扩展的需求和依赖项。请阅读此 web tip /wiki 以设置您应用程序 composer.json 的 minimum-stability 设置。

运行以下命令之一:

$ php composer.phar require kartik-v/yii2-validators "@dev"

"kartik-v/yii2-validators": "@dev"

将以下内容添加到您的 composer.json 文件的 require 部分中。

使用方法

EmailValidator

此类扩展了 yii\validators\EmailValidator 类,以提供多个电子邮件验证支持。可以通过在模型验证规则中使用别名 k-email 容易地使用 EmailValidator 类。例如,在您的模型中,您可以像以下示例那样使用它

use yii\db\ActiveRecord;

class EmailModel extends ActiveRecord {
    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [['to', 'cc', 'bcc'], 'k-email', 'allowName' => true, 'enableIDN' => true, 'max' => 5],
        ];
    }
}

在您的视图中,当您使用 ActiveForm 渲染模型输入时,您可以设置 enableClientValidation 以控制是否需要客户端验证。

// views/email/send.php

use yii\widgets\ActiveForm;

$form = ActiveForm::begin(['enableClientValidation' => true]);
echo $form->field($model, 'to')->textInput();
echo $form->field($model, 'cc')->textInput();
echo $form->field($model, 'bcc')->textInput();
// other fields
ActiveForm::end();

PhoneValidator

此类扩展了 yii\validators\Validator 类,使用基于 Google 的 libphonenumber 库的 libphonenumber-for-php 验证电话号码。可以通过在模型验证规则中使用别名 k-phone 容易地使用 PhoneValidator 类。例如,在您的模型中,您可以像以下示例那样使用它

use yii\db\ActiveRecord;

class ContactModel extends ActiveRecord {
    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [['phone1'], 'k-phone', 'countryValue' => 'US'],
            [['phone2'], 'k-phone', 'countryAttribute' => 'country', 'applyFormat' => false],
        ];
    }
}

CardValidator

CardValidator 继承了 yii\validators\Validator 组件。它使用 Luhn 校验和验证标准借记卡和信用卡号码输入。它还帮助自动检测卡片类型,并进一步验证输入的持卡人姓名、有效期和 CVV。在您的模型验证规则中,可以通过别名 k-card 轻松使用 CardValidator 类。例如,在您的模型中,您可以使用如下所示:

use yii\db\ActiveRecord;

class PaymentModel extends ActiveRecord {
    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [
                ['card_number'], 
                'k-card', 
                'typeAttribute' => 'card_type', 
                'holderAttribute' => 'holderName',
                'expiryYearAttribute' => 'expiryYear',
                'expiryMonthAttribute' => 'expiryMonth',
                'cvvAttribute' => 'cvv',
            ],
        ];
    }
}

JsonValidator

JsonValidator 继承了 yii\validators\Validator 组件。它验证文本区域字段中的 JSON 输入。验证器还通过 prettify 设置帮助将 JSON 输入覆盖为格式化的输出。它包括服务器端和客户端验证。在您的模型验证规则中,可以通过别名 k-json 轻松使用 JsonValidator 类。例如,在您的模型中,您可以使用如下所示:

use yii\db\ActiveRecord;

class ProductModel extends ActiveRecord {
    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [
                ['products_list_json'], 
                'k-json', 
                'prettify' => true, // set this to false if you do not wish to prettify the json input
            ],
        ];
    }
}

许可

yii2-validators 采用 BSD-3-Clause 许可发布。有关详细信息,请参阅附带文件 LICENSE.md