CActiveForm + iso-8859-1 = Invalid UTF-8 sequence

Hi!

The charset of my yii site is ‘iso-8859-1’ and I’d like to use CActiveForm.




    <?php $form=$this->beginWidget('CActiveForm', array(

                                    'id'=>'comment-form',

                                    'enableAjaxValidation'=>true,

                                    'clientOptions'=>array('validateOnSubmit'=>true, 'validateOnChange'=>false),

    )); ?>


...


    <?php $this->endWidget(); ?>



When I hit the submit button, the AJAX response is the following:




PHP Error

Description


json_encode() [<a href='function.json-encode'>function.json-encode</a>]: Invalid UTF-8 sequence in argument

Source File


/var/www/test/yii-1.1.4.r2429/framework/web/widgets/CActiveForm.php(595)


00583:     {

00584:         $result=array();

00585:         if(!is_array($models))

00586:             $models=array($models);

00587:         foreach($models as $model)

00588:         {

00589:             if($loadInput && isset($_POST[get_class($model)]))

00590:                 $model->attributes=$_POST[get_class($model)];

00591:             $model->validate($attributes);

00592:             foreach($model->getErrors() as $attribute=>$errors)

00593:                 $result[CHtml::activeId($model,$attribute)]=$errors;

00594:         }

00595: return function_exists('json_encode') ? json_encode($result) : CJSON::encode($result);

00596:     }

00597: }


Stack Trace


#0 /var/www/test/yii-1.1.4.r2429/framework/web/widgets/CActiveForm.php(595): json_encode()

#1 /var/www/test/protected/controllers/VelemenyController.php(619): validate()

#2 /var/www/test/protected/controllers/VelemenyController.php(571): VelemenyController->newComment()

#3 /var/www/test/yii-1.1.4.r2429/framework/web/actions/CInlineAction.php(50): VelemenyController->actionRvList()

#4 /var/www/test/yii-1.1.4.r2429/framework/web/CController.php(300): CInlineAction->run()

#5 /var/www/test/yii-1.1.4.r2429/framework/web/CController.php(278): VelemenyController->runAction()

#6 /var/www/test/yii-1.1.4.r2429/framework/web/CController.php(257): VelemenyController->runActionWithFilters()

#7 /var/www/test/yii-1.1.4.r2429/framework/web/CWebApplication.php(324): VelemenyController->run()

#8 /var/www/test/yii-1.1.4.r2429/framework/web/CWebApplication.php(121): CWebApplication->runController()

#9 /var/www/test/yii-1.1.4.r2429/framework/base/CApplication.php(135): CWebApplication->processRequest()

#10 /var/www/test/indexy.php(14): CWebApplication->run()



It seems that json_encode() was given some not UTF-8 encoded data. (according to php manual json_encode supports only utf-8)

How could I use CActiveForm with ‘charset’ => ‘iso-8859-1’?

Welcome to the encoding mismatch hell :).

Problem here is that strings in JSON objects are always UTF-8 encoded. So you need to convert back and forth between ISO-8859-1 and UTF-8 as soon as JSON is involved. Any chance you move to UTF-8 with the whole site? That would avoid such problems in the first place.

Thanks for the answer. Unfortunately I can’t change the charset of the site to UFT-8 due to compatibility reasons.

Basically I’ve to change only the CActiveForm::validate function :)

I submitted a new issue and my bugfix.

http://code.google.com/p/yii/issues/detail?id=1654