2016-08-24 30 views
1

我在我所使用ckeditor警予文本字段validtaion

<div class="row"> 
    <?php echo $form->labelEx($model,'text'); ?> 
    <?php echo $form->textArea($model, 'text', array('id'=>'editor1')); ?> 
    <?php echo $form->error($model,'text'); ?> 
</div> 

<script type="text/javascript"> 
    CKEDITOR.replace('editor1'); 
</script> 

规则在模型中的text field

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


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

这工作得很好

1)当我离开现场blnk我得到所需的文本错误。

2)当我写的话他们少5个或多于4000我得到想要的错误

,但是当我插入一些空白空间,然后我没有得到任何错误,并提交表单。

请帮助任何帮助将不胜感激!

+0

尝试用$ total_words = str_word_count(修剪($这个 - >文本)); –

回答

0

您可以使用正则表达式来删除你的函数multipe空白字符

public function validateWordLength($attribute,$params) 
{ 
    $this->text = preg_replace('/\s+/', ' ',$this->text); 
    $total_words= str_word_count($this->text); 
    if($total_words>4000) 
    { 
     $this->addError('text', 'Your description length is exceeded'); 
    } 
    if($total_words<5) 
    { 
     $this->addError('text', 'Your description length is too small'); 
    } 
}