Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Formating of error messages will fail for some rules if input is invalid UTF-8

未关闭
#825 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
php
领域
backend

调研方向

Start by reproducing the Regex rule example with the invalid UTF-8 value "\x80", then trace the error-message formatting path that passes the value to IntlMessageFormatter::format(). Verify the fix by confirming that the result is formatted as "Field not valid." rather than returning the unformatted template, and add coverage for the invalid input if the existing test structure provides a suitable place.

由索引模型根据 Issue 内容生成。

描述

Description

If a value provided to validation rule is incorrect UTF-8,
then formatting of error messages for some rules will fail and original template message will be returned, without formatting.

For example, rule:

'field' => new Regex(
    pattern: '/^abc$/u',
),

input field's value: "\x80" (it is invalid UTF-8)

result error: "{Property} not valid.", instead of "Field not valid."

This happens because for some rules value is passed to formatting along with other parameters:
['property' => ..., 'Property' => ..., 'value' => ...]

IntlMessageFormatter::format() fails to format it and returns false, and not formatted message used as fallback,
it fails because underlying intl ext (ICU library) strictly requires all incoming payload text to be valid UTF-8 or UTF-16

But error message should not be corrupted because of user input, value can be sanitized like this:

mb_substitute_character(0xFFFD); 
$clean = mb_convert_encoding($dirty, 'UTF-8', 'UTF-8'); 

0xFFFD is official Unicode Replacement Character: �

or function from intl ext can be used for same result,
it is better because mb_substitute_character() sets global state

$clean = UConverter::transcode($dirty, 'UTF-8', 'UTF-8');

https://www.php.net/manual/en/uconverter.transcode.php

If the input string contains a sequence of bytes which is not valid in the encoding specified by fromEncoding, they are replaced by Unicode code point U+FFFD (Replacement Character) before converting to toEncoding.

Package version

No response

PHP version

No response

主要语言
PHP
星标
166
派生
47
平均合并
1 天 7 小时
30 天内合并 PR
1

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

yiisoft/validator 的其他 Issue

查看 yiisoft/validator 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。