首页 > 解决方案 > 显示通用消息而不是特定于约束的消息

问题描述

对于具有minLength: 2maxLength: 10符号约束的输入字段,如果您要引入 11 个符号,您将收到一条静态错误消息,指出它是“无效条目”。

以下Input字段:

<Input value="{
  path: 'modelExample>/Example1',
  type: 'sap.ui.model.type.String',
  constraints: {
    minLength: 2,
    maxLength: 10
  }
}"/>

如何修改输入字段,以便将错误消息更改为:“您的输入只能介于 2 到最多 10 个符号之间”?

标签: sapui5

解决方案


您必须在某处Error手动设置 valueState 而不是让框架自动生成消息。

在这里,您可以看到消息根据输入值正确显示:

sap.ui.require([
  "sap/ui/core/Core"
], Core => Core.attachInit(() => sap.ui.require([
  "sap/ui/core/mvc/XMLView",
], async (XMLView) => {
  "use strict";
  
  const control = await XMLView.create({
    definition: `<mvc:View xmlns:mvc="sap.ui.core.mvc"
      xmlns="sap.m"
      xmlns:core="sap.ui.core"
      core:require="{ ThatTypeString: 'sap/ui/model/type/String' }">
      <App>
        <Page showHeader="false" class="sapUiResponsiveContentPadding">
          <Input width="17rem" value="{
            value: '1234567890',
            type: 'ThatTypeString',
            constraints: {
              minLength: 2,
              maxLength: 10
            }
          }"/>
        </Page>
      </App>
    </mvc:View>`,
    height: "100%",
  });

  Core.getMessageManager().registerObject(control, true);
  control.placeAt("content");
})));
<script id="sap-ui-bootstrap"
  src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js"
  data-sap-ui-libs="sap.ui.core,sap.m"
  data-sap-ui-theme="sap_fiori_3"
  data-sap-ui-async="true"
  data-sap-ui-compatversion="edge"
  data-sap-ui-excludejquerycompat="true"
  data-sap-ui-xx-waitfortheme="init"
></script>
<body id="content" class="sapUiBody sapUiSizeCompact"></body>

  • UI5 输入验证最小长度消息
  • UI5 输入验证最大长度消息

请参阅文档主题验证消息部分“自动创建的消息”。大多数情况下,在文件中设置"handleValidation": true为就足够了。"sap.ui5"manifest.json

不要手动设置 valueState,而是确保框架自动设置消息。


推荐阅读