yii2-validators
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, (*1)
Recaptcha validator and InputWidget is having new features:
- Compatible with Pjax
- Added ability to have more than one ReCaptcha widget on same page, (*2)
Installation
The preferred way to install this extension is through composer. Check the composer.json for this extension's requirements and dependencies., (*3)
To install, either run, (*4)
$ php composer.phar require bitdevelopment/yii2-validators "v1.1.0"
or add, (*5)
"bitdevelopment/yii2-validators": "v1.1.0"
to the require
section of your composer.json
file., (*6)
Latest Release
NOTE: The latest version of the module is v1.1.0., (*7)
Usage
WordCount Validator
Add validator to your rules list in your model, (*8)
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:, (*9)
$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., (*10)
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, (*11)
Setting up in params
In your params.php
file of your project setup new key so it will look like this:, (*12)
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., (*13)
Now let's setup validation in your Model:, (*14)
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:, (*15)
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:, (*16)
//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) ?>