Is CStringValidator UTF-8 aware?

Seems not.

Because I can’t find any other explanation for CStringValidator halting form processing, claiming that field cannot exceed 70 characters, while text entered to it has actually 54 characters (19 of them - UTF-8 encoded national characters).

Here’s dirty screen capture with Polish translation, but fairly good explaining, what I’m talking about.

2217

yii-validator.png

Is this situation normal?

Unfortunately you’re right.

Simple test:




$s = "abcd";

echo mb_strlen($s); // 4

$s = "чћшж";

echo mb_strlen($s); // 8



Once again, I’m asking without doing a good search! :expressionless:

Well…


$s = "abcd";

echo mb_strlen($s, 'UTF-8'); // 4

$s = "чћшж";

echo mb_strlen($s, 'UTF-8'); // 4

So (CStringValidator.encoding):


array('LOGN, IMIE', 'length', 'max'=>20, 'encoding'=>'UTF-8')

instead of:


array('LOGN, IMIE', 'length', 'max'=>20)

solved the problem!

Interesting thing is that if CStringValidator.encoding is not set, the application charset will be used. Basically that means UTF-8 will be used, unless you’ve changed Yii::app()->charset to some other encoding. So if you used default app charset I don’t know why this issue happened.

Are you sure? I’ve heard (on this forum, but in some other topic) that in situation, you describe an internal PHP encoding will be used, not Yii’s encoding! That could be the true, since I have my Yii application coded in UTF-8, but even so, with encoding not set in CStringValidator, I had a strange results, as described. This would rather support, that some different, internal encoding is used, not UTF-8.

Behavior of CStringValidator is related to framework version:

  • 1.1.0 - 1.1.6 - using PHP internal encoding if CStringValidator::encoding is not set
  • 1.1.7+ - using CApplication::charset if CStringValidator::encoding is not set

I see… That’s explains much. I’m all the time at 1.1.4! :]

So now you have a reason to do an upgrade of framework :)

Unfortunately, this decision is far beyond me. I personally don’t need any special reason. The only reason, for me, upgrade is the fact that new version has been released! :]