单词的最小和最大规则?

时间:2016-08-24 05:15:49

标签: php yii

Yii中,有任何rule应该定义文本字段中的最小和最大字数

我用过这个

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

但这适用于charecters我需要最少4个单词和最多4000个单词而不是character

1 个答案:

答案 0 :(得分:3)

您可以在YII端制作验证字长的自定义规则,我正在考虑文本区域名称是描述。

public function rules()
{
    return array(
       array('description', 'validateWordLength'),
    );
}

现在你唯一需要做的就是在模型中创建一个新方法,以你刚刚声明的验证规则命名。

public function validateWordLength($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');
    } 
}