bitdevelopment/yii2-validators 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bitdevelopment/yii2-validators

Composer 安装命令:

composer require bitdevelopment/yii2-validators

包简介

Set of validators that doesn't exist in Yii by default

README 文档

README

Yii2-validators are validators for yii for some specific cases that are not covered by standard yii2 validators. Currently it holds 2 validators but will be extended in future.

  • WordCount Validator
  • ReCaptcha Validator and InputWidget

Recaptcha validator and InputWidget is having new features:

  • Compatible with Pjax
  • Added ability to have more than one ReCaptcha widget on same page

Installation

The preferred way to install this extension is through composer. Check the composer.json for this extension's requirements and dependencies.

To install, either run

$ php composer.phar require bitdevelopment/yii2-validators "v1.1.0"

or add

"bitdevelopment/yii2-validators": "v1.1.0"

to the require section of your composer.json file.

Latest Release

NOTE: The latest version of the module is v1.1.0.

Usage

WordCount Validator

Add validator to your rules list in your model

use bitdevelopment\yii2validators\WordValidator;

class Post extends \yii\base\Model {

  public function rules() {
    return [
            [['myField'],WordValidator::className(),'min'=>200,'max'=>500]
    ];
  }
  ...

If you want to validation occurs while user is typing you can use validateOnType option, for example:

$form->field($model, 'comment',['validateOnType' => true])->textarea([
                    'rows'=> 15
            ]);

Since version v1.1.0 stripTags feature has been added, that strips HTML tags before it counts words, by default stripTags is setup to false.

ReCaptcha Validator and Widget setup

There are two ways of setting ReCaptcha Validator in your project, one is inline setup, if you decide to use multiple key:secret pairs and setup in your params.php file if you have global setup

Setting up in params

In your params.php file of your project setup new key so it will look like this:

return [
       /*
	* Your params...
	*/
 	'reCaptcha' => [
        	'siteKey' => 'site_key',
        	'secret' => 'secret_key',
    	],
];

Note that site_key and secret_key you can obtain on your Google ReCaptcha Admin.

Now let's setup validation in your Model:

use bitdevelopment\yii2validators\ReCaptchaValidator;

class Post extends \yii\base\Model {

  public $reCaptcha;

  public function rules() {
    return [
            [['reCaptcha'], ReCaptchaValidator::className()],
	    [['reCaptcha'], 'required'],
    ];
  }
  ...

And Last step is to setup inputWidget on your view file, where your form is:

echo $form->field($comments, 'reCaptcha')->widget(
            	bitdevelopment\yii2validators\ReCaptcha::className(),
        )->label(false) ?>
Inline Settings

Similarly like when setting up with params.php you can setup params inline against a field, like in this example:

  //In your rules
  [['reCaptcha'], ReCaptchaValidator::className(),'secret'=>'secret_key'],
  ...
  //In your view
  echo $form->field($comments, 'reCaptcha')->widget(
            	bitdevelopment\yii2validators\ReCaptcha::className(),
		['siteKey','site_key']
        )->label(false) ?>

统计信息

  • 总下载量: 69
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-12-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固