CStringValidator длина строки

в rules модели


array('title','length','max'=>255),

проверка длинны строки, кодировка utf-8

При вводе русских символов неправильно вычисляет длину строки. Кто нибудь пробовал с этим бороться?

Мне кажется что надо переписать функцию validateAttribute в классе CStringValidator, вот только как это сделать не внося изменения в сам фрамеворк?

расширить CStringValidator

подгрузить валидотор в Rules, что-то вроде:


 public function rules()

{

    return array(

        'MyStringValidator', 'StringValidator', 'length', 'max' => 255);

}

Создать файл mbstring.php и положить, например, в components




class mbstring extends CValidator {

    public $max; // maximum allowed string length

    public $min; // minimum allowed string length

    public $islength; // required exact length


    public $tooShort; // custom message for short string

    public $tooLong; // custom message for long string

    public $wrongCharset;  // custom message for wrong character set

    public $allowEmpty=true;


    protected function validateAttribute($object,$attribute) {

        

        if (!function_exists("mb_internal_encoding")) {

            $message = CustomUtils::t('Multibyte functions are not available: check if mbstring php extension is installed.');

            $this->addError($object,'',$message);

            return;

        }


        mb_internal_encoding(Yii::app()->charset);

        $value = $object->$attribute;

        if($this->allowEmpty && ($value === null || $value === ''))

            return;

        if (!mb_check_encoding($value)) {

            $message=$this->wrongCharset !== null ? $this->wrongCharset : CustomUtils::t('{attribute} has wrong character set.');

            $this->addError($object,$attribute,$message);

        }

        $length = mb_strlen($value);

        if($this->min !== null && $length < $this->min) {

            $message=$this->tooShort!==null?$this->tooShort:CustomUtils::t('{attribute} is too short (minimum is {min} characters).');

            $this->addError($object,$attribute,$message,array('{min}'=>$this->min));

        }

        if($this->max!==null && $length>$this->max) {

            $message=$this->tooLong!==null?$this->tooLong:CustomUtils::t('{attribute} is too long (maximum is {max} characters).');

            $this->addError($object,$attribute,$message,array('{max}'=>$this->max));

        }

        if($this->islength!==null && $length!==$this->islength) {

            $message=$this->message!==null?$this->message:CustomUtils::t('{attribute} is of the wrong length (should be {length} characters).');

            $this->addError($object,$attribute,$message,array('{length}'=>$this->islength));

        }

    }

}



И в правилах писать:




public function rules() {

        return array(

            array('text', 'mbstring', 'min'=>1, 'max'=>500),

        );



Спасибо :rolleyes: