EWordValidator validates that the attribute value has a specific words count and checks this value against whitelist and blacklist.
Tested in Yii 1.1.10, but should work starting from Yii 1.1.7
Extract the archive and put the file under protected/extensions directory. (Or under protected/extensions/validators to keep things organized)
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*/), ); }
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}' ), ),
Added ability to filter data before validation.
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.
Total 2 comments
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.
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:
Next I changed the getLength function as follows:
Usage where html tags are ignored:
Leave a comment
Please login to leave your comment.