thyseus/yii2-word-validator

此包最新版本(v0.1.0)没有可用的许可信息。

Yii 2 验证器,用于验证字符串内容数量、黑名单和白名单。

安装: 3,573

依赖: 0

建议者: 0

安全: 0

星星: 0

分支: 0

类型:yii2-extension

v0.1.0 2017-02-15 10:00 UTC

This package is auto-updated.

Last update: 2024-09-06 23:36:59 UTC


README

WordValidator 验证属性值是否有特定的单词数量,并检查此值是否在白名单和黑名单中。它是从 Anton Yakushin 的 Yii 1 版本移植到 Yii 2 的 https://github.com/antonCPU/word-validator

需求

在 Yii 2.0.9 中进行了测试。

安装

composer require thyseus/yii2-word-validator

使用方法

将以下代码添加到您的模型类 rules() 方法中

use thyseus\validators\WordValidator;

public function rules()
{
   return [
       //other validators...
       array('attributeName', WordValidator::className(), /*add custom rules here*/),
   );
}

例如

use thyseus\validators\WordValidator;

public function rules() {
  return [
    [['message'], WordValidator::className(),
    'min' => 2,
    'max' => 5,
    'whitelist' => array('please', 'test'),
    'blacklist' => array('restricted', 'email.*'), //could be a regular expression
    'messages'  => array(
        'max' => '{attribute} is too long (maximum is {max} words, but now it\'s {length})'
        ),
    ];
  ];

验证规则

  • max - 属性应包含的单词数量少于(或等于)此值;
  • min - 最小单词数量;
  • exact - 预期只有这个单词数量;
  • blacklist - 属性中不应包含的单词数组。
              There also could be a regular expression;
    
  • whitelist - 属性中必须至少包含这些单词/表达式之一。

消息

可以使用 messages 参数覆盖任何默认错误消息。所有消息都支持 {attribute} 和 {length} 占位符。每个验证方法都会将其值添加到相应的占位符(与名称相同)。对于 min 规则,消息可以指定为

[/*...*/
    'messages' => array(
        'min' => 'Your {attribute} is now has {length} words. But should be
             at least {min}'
    ),
],

支持客户端验证。