Yii 1.1: word-validator

Validates words count, blacklist and whitelist.

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*/),

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.


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}'

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.


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

#9949 report it
antoncpu at 2012/09/24 03:31pm
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.

#9813 report it
J. Michael Moore at 2012/09/12 04:41pm
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());
            $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})'

Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: New BSD License
  • Developed by: antoncpu
  • Category: Validation
  • Votes: +3 / -1
  • Downloaded: 540 times
  • Created on: Feb 27, 2012
  • Last updated: Oct 7, 2012