php - yii text field validtaion with ckeditor -
i have text field
in have used 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>
rules in model is
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 exceeded'); } if($total_words<5) { $this->adderror('text', 'your description length small'); } }
this works fine
1) when leave field blnk required text error.
2) when write words less them 5 or more 4000 desired error
but when insert blank space not getting error , form submitted.
please appreciated!!
you can use regex remove multipe whitespaces in function
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 exceeded'); } if($total_words<5) { $this->adderror('text', 'your description length small'); } }
Comments
Post a Comment