我有一个使用CKEditor的文本字段:
<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>
模型中的规则为
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)当我将字段留空时,出现必填文本错误。
2)当我写的单词少于5个或多于4000个时,我得到期望的错误
但是,当我插入一些空格时,我没有收到任何错误,并且提交了表单。
您可以使用正则表达式删除函数中的多重空格
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');
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句