EWordValidator validates that the attribute value has a specific words count and checks this value against whitelist and blacklist.
Requirements ¶
Tested in Yii 1.1.10, but should work starting from Yii 1.1.7
Installation ¶
Extract the archive and put the file under protected/extensions directory. (Or under protected/extensions/validators to keep things organized)
Usage ¶
Add the following code to your model class rules() method
public function rules()
{
return array(
//other validators...
array('attributeName', 'ext.EWordValidator'/*,add here needed rules*/),
);
}
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:
array(/*...*/
'messages' => array(
'min' => 'Your {attribute} is now has {length} words. But should be
at least {min}'
),
),
Filter (since 1.1) ¶
Added ability to filter data before validation.
- filter - a valid php callback. Takes a model attribute as a first argument.
- filterClient - a valid javascript callback.
Example ¶
Check if a "body" attribute has from 2 to 5 words count, contains either the word "please" or "test" and does not contain a word "restricted" and "email.*" expression. Also the default message for "max" rule is overridden.
array('body', 'ext.EWordValidator',
'min' => 2,
'max' => 5,
'whitelist' => array('please', 'test'),
'blacklist' => array('restricted', 'email.*'),
'messages' => array(
'max' => '{attribute} is too long (maximum is {max} words,
but now it\'s {length})'
),
),
Also a client side validation is supported.
Ignoring HTML tags
This extension is great. However, I needed it to ignore html tags when counting words.
Here are the modifications I did if you want to incorporate them into your package.
I added the variable to the EWordValidator class and set the default to true so that omitting the parameter would mimic the current behavior:
public $countTags = true; /** * If true, count html tags, otherwise, do not count html tags * @var type */
Next I changed the getLength function as follows:
protected function getLength() { if (null === $this->_length) if ($this->countTags) $this->_length = str_word_count($this->getSource()); else $this->_length = str_word_count(strip_tags($this->getSource())); return $this->_length; }
Usage where html tags are ignored:
array('body', 'ext.EWordValidator', 'min' => 2, 'max' => 5, 'countTags' => false, 'whitelist' => array('please', 'test'), 'blacklist' => array('restricted', 'email.*'), 'messages' => array( 'max' => '{attribute} is too long (maximum is {max} words, but now it\'s {length})' ), ),
Ignoring HTML tags
Hi Michael! Thanks for the idea and implementation.
I would recommend to override getSource() method as in this case you'll get clean data for other validation methods such as white and black lists.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.