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,235oCurrencyFormat.parse("$1,234.57"); // returns [1234.57, "USD"] oCurrencyFormat.parse("¥1,235"); // returns [1235, "JPY"]貨幣格式的以下格式選項可用:
-
currencyCode 定義當 showMeasure 設置為 true 時是否使用代碼或符號。
-
trailingCurrencyCode 定義貨幣代碼是否始終顯示在金額之后,與區域設置無關。
-
currencyContext 定義了用于格式化貨幣編號的模式。 它可以設置為標準(默認)或會計。
總結
以上是生活随笔為你收集整理的SAP UI5 Currency 数据类型的校验逻辑分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎查询地址的经纬度
- 下一篇: Centeros常用命令