Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://gitlab.com/thyseus/yii2-word-validator
https://gitlab.com/thyseus/yii2-word-validator
Last synced: 13 days ago
JSON representation
- Host: gitlab.com
- URL: https://gitlab.com/thyseus/yii2-word-validator
- Owner: thyseus
- Created: 2018-06-06T08:06:20.490Z (over 6 years ago)
- Default Branch: master
- Last Synced: 2024-10-31T06:51:37.243Z (20 days ago)
- Stars: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
Awesome Lists containing this project
README
Word Validator
==============WordValidator validates that the attribute value has a specific words count and checks this value against whitelist and blacklist.
It is a port of https://github.com/antonCPU/word-validator from Anton Yakushin from Yii 1 to Yii 2.##Requirements
Tested in Yii 2.0.9.
##Installation
composer require thyseus/yii2-word-validator
##Usage
Add the following code to your model class rules() method
~~~
use thyseus\validators\WordValidator;public function rules()
{
return [
//other validators...
array('attributeName', WordValidator::className(), /*add custom rules here*/),
);
}~~~
For example:
~~~
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})'
),
];
];
~~~#### Validation rules
- **max** - the attribute should contain less (or equal) words count;
- **min** - minimum words count;
- **exact** - expected only this words count;
- **blacklist** - array of words that should not be in the attribute.
There also could be a regular expression;
- **whitelist** - at least one of these words/expressions must be in the attribute.#### Messages
Any default error message could be overridden using **messages** parameter.
All messages support {attribute} and {length} placeholders. Each validation
method adds it's value to a correspond (the same as a name) placeholder.
For **min** rule a message could be specified as:
~~~
[/*...*/
'messages' => array(
'min' => 'Your {attribute} is now has {length} words. But should be
at least {min}'
),
],
~~~Client side validation is supported.