user1234 user1234 - 3 months ago 20
PHP Question

Minimum and Maximum rule for words?

In

Yii
is there is any
rule
which should define min and max number of words in textfield

I used this

array('text', 'length', 'min'=>5, 'max'=>40000),


but this is for
charecters
i need min 4 words and max 4000 words instead of
character

Answer

You Can make custom rule for validate word length on YII side, I am considering text area name is description.

public function rules()
{
    return array(
       array('description', 'validateWordLength', 'strength'=>self::STRONG),
    );
}

Now the only thing you need to do is create a new method inside the model, named after the validation rule you just declared.

public function passwordStrength($attribute,$params)
{
    $total_words= str_word_count($object->$attribute);
    if($total_words>4000)
    {
       $this->addError($attribute, 'Your description length is exceeded');
    }
    if($total_words<5)
    {
       $this->addError($attribute, 'Your description length is too small');
    } 
}