Yii2: "Malformed utf-8 characters possibly incorrectly encoded" using NumberValidator in a yii2 project with charset set to iso-8859-1

Created on 5 Feb 2016  路  5Comments  路  Source: yiisoft/yii2

I think that there is a bug in the BaseJson.php when returning the client validator in a project with iso-8859-1 charset.

The form field throws an invalid param exception if the rule for user_id was set to 'integer'.

<?= $form->field($model, 'user_id') ?>

I don't know how to solve this problem, then I modify the static method encode of the BaseJson.php as follows:

public static function encode($value, $options = 320)
    {
        if(\Yii::$app->charset == "iso-8859-1")
            if(isset($value['message']))
                $value['message'] = utf8_encode($value['message']);
...
   }
docs

Most helpful comment

I do not think Yii should handle this internally. JSON is always UTF8 so you have to ensure to only pass UTF-8 strings to a Json encoding method. Will add a note to the docs. Thanks for reporting!

All 5 comments

Right. JSON must be UTF-8 so I guess it's having trouble in case input isn't UTF-8. The check could be

if (\Yii::$app->charset !== "utf-8")

thanks.

Looks not as simple. Incoming encoding could be anything. Also utf8_encode works with iso-8859-1 only which isn't a good option for all other non UTF-8 encodings.

I do not think Yii should handle this internally. JSON is always UTF8 so you have to ensure to only pass UTF-8 strings to a Json encoding method. Will add a note to the docs. Thanks for reporting!

My website has this problem very frequently

2017-06-08 01:04:33 [221.195.61.147][-][f2f6823b8b5ee54d735e16b6f482d4dd][error][yii\base\InvalidParamException] yii\base\InvalidParamException: Malformed UTF-8 characters, possibly incorrectly encoded. in /home/flxx/basic/vendor/yiisoft/yii2/helpers/BaseJson.php:132
Stack trace:
#0 /home/flxx/basic/vendor/yiisoft/yii2/helpers/BaseJson.php(66): yii\helpers\BaseJson::handleJsonError(5)
#1 /home/flxx/basic/models/info/PostLog.php(50): yii\helpers\BaseJson::encode(Array, 256)
#2 /home/flxx/basic/events/globalEvent.php(190): app\models\info\PostLog->addOne('_csrf error', 0)
#3 [internal function]: app\events\globalEvent::afterRequest(Object(yii\base\Event))
#4 /home/flxx/basic/vendor/yiisoft/yii2/base/Component.php(545): call_user_func(Array, Object(yii\base\Event))
#5 /home/flxx/basic/vendor/yiisoft/yii2/base/Application.php(383): yii\base\Component->trigger('afterRequest')
#6 /home/flxx/basic/web/index.php(14): yii\base\Application->run()
#7 {main}
2017-06-08 01:04:33 [221.195.61.147][-][f2f6823b8b5ee54d735e16b6f482d4dd][info][application] $_POST = [
    '_csrf' => 'b3BYdVF6TllfESs6EA8UHF8XE00VHmM.FQBsNnwcNwYhFCoDO0kXCQ=='
    'RegisterForm' => [
        'corpName' => '蹞驯規山袠埽肢摔虓芎械詯袨佾司屎雍'
        'username' => 'hbjx'
        'email' => '[email protected]'
        'password' => 'd6398915'
        'aff' => ''
        'verifyCode' => ''
    ]
    'btn' => 'b艽注印'
]

$_COOKIE = [
    '_csrf' => 'd9f2a09a733a075448a9602b80f4d441d14cb5f95d12dbab809562cb02dfe028a:2:{i:0;s:5:\"_csrf\";i:1;s:32:\"5rZheUGmw4rUDt01-OlNCwESApEOxe73\";}'
    'PHPSESSID' => 'f2f6823b8b5ee54d735e16b6f482d4dd'
]

The Server Information

$_SERVER = [
    'USER' => 'apache'
    'HOME' => '/usr/share/httpd'
    'HTTP_ACCEPT_ENCODING' => 'gzip, deflate'
    'HTTP_CONTENT_LENGTH' => '370'
    'HTTP_HOST' => 'www.****.com'
    'HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded'
    'HTTP_COOKIE' => '_csrf=d9f2a09a733a075448a9602b80f4d441d14cb5f95d12dbab809562cb02dfe028a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%225rZheUGmw4rUDt01-OlNCwESApEOxe73%22%3B%7D;PHPSESSID=f2f6823b8b5ee54d735e16b6f482d4dd'
    'HTTP_ACCEPT_LANGUAGE' => 'zh-cn'
    'HTTP_CACHE_CONTROL' => 'no-cache'
    'HTTP_ACCEPT' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*'
    'HTTP_USER_AGENT' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'
    'HTTP_REFERER' => 'https://www.****.com/user/register'
    'REDIRECT_STATUS' => '200'
    'SERVER_NAME' => '****.com'
    'SERVER_PORT' => '80'
    'SERVER_ADDR' => '100.100.100.100'
    'REMOTE_PORT' => '30419'
    'REMOTE_ADDR' => '221.195.61.000'
    'SERVER_SOFTWARE' => 'nginx/1.10.1'
    'GATEWAY_INTERFACE' => 'CGI/1.1'
    'REQUEST_SCHEME' => 'http'
    'SERVER_PROTOCOL' => 'HTTP/1.1'
    'DOCUMENT_ROOT' => '/home/flxx/basic/web'
    'DOCUMENT_URI' => '/index.php'
    'REQUEST_URI' => '/user/register'
    'SCRIPT_NAME' => '/index.php'
    'CONTENT_LENGTH' => '370'
    'CONTENT_TYPE' => 'application/x-www-form-urlencoded'
    'REQUEST_METHOD' => 'POST'
    'QUERY_STRING' => ''
    'SCRIPT_FILENAME' => '/home/flxx/basic/web//index.php'
    'FCGI_ROLE' => 'RESPONDER'
    'PHP_SELF' => '/index.php'
    'REQUEST_TIME_FLOAT' => 1496855073.725
    'REQUEST_TIME' => 1496855073
]
Was this page helpful?
0 / 5 - 0 ratings