2016-08-24 24 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'); 
    } 
}