SAP UI5 Currency 数据类型的校验逻辑分析

JerryWang_汪子熙 -
SAP UI5 Currency 数据类型的校验逻辑分析

按照 Jerry 这篇文章介绍的代码,运行之后,给类型为 sap.ui.model.type.Currency 的字段设置一个非 number 类型的值之后,触发该数据类型自带的数据校验机制,显示 Enter a number 的错误消息。

SAP UI5 应用开发教程之四十六 - 使用 Message Manager 实现开箱即用的验证(Validation)信息抛出

调试入口在 CompositeBinding.prototype.setExternalValue 函数内部。

if (this.oType) {
            pValues = SyncPromise.resolve().then(function() {
                var aCurrentValues;
                if (that.oType.getParseWithValues()) {
                    aCurrentValues = that.getCurrentValues();
                }
                return that.oType.parseValue(oValue, that.sInternalType, aCurrentValues);

outputFormat 的具体实现是 NumberFormat

NumberFormat 是一个静态类,用于根据一组格式选项格式化和解析数值。

数据格式化是 SAPUI5 中的关键功能之一,它使应用程序能够根据用户区域设置显示数据。 为此,SAPUI5 使用通用区域设置数据存储库 (CLDR),这是一个提供特定区域设置模式的第三方库。 SAPUI5 使用这些模式来适应不同语言的约定。

数据格式化的一种用例是格式化和解析数字(包括货币信息)的能力。 对于这个特定的用例,CLDR 提供带有预配置货币信息的模式,例如一组不同货币的小数位数。 也可以通过添加新的自定义货币或重新配置现有货币来定义自定义货币。

其中匹配整形数值的正则表达式,维护在变量 sRegExpInt 内:^\s*([\+\+⁺₊➕﬩﹢+\-\-‐‒–⁻₋−➖﹣-]?[0-9,]+)\s*$

正则表达式验证失败:

因此抛出 ParserException 异常:

从 library resource bundle 里取得占位符 EnterNumber 的文本:Enter a number

关于 NumberFormat 更多的例子:

// "NumberFormat" required from module "sap/ui/core/format/NumberFormat"
var oCurrencyFormat = NumberFormat.getCurrencyInstance({
    currencyCode: false
});

oCurrencyFormat.format(1234.567, "USD"); // returns $1,234.57
oCurrencyFormat.format(1234.567, "JPY"); // returns ¥1,235

oCurrencyFormat.parse("$1,234.57"); // returns [1234.57, "USD"]
oCurrencyFormat.parse("¥1,235"); // returns [1235, "JPY"]

货币格式的以下格式选项可用:

currencyCode 定义当 showMeasure 设置为 true 时是否使用代码或符号。trailingCurrencyCode 定义货币代码是否始终显示在金额之后,与区域设置无关。currencyContext 定义了用于格式化货币编号的模式。 它可以设置为标准(默认)或会计。
特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。

Tags 标签

javascript前端html5htmlsap

扩展阅读

加个好友,技术交流

1628738909466805.jpg