客户端 API 控件

Power Pages客户端 API 控件实现Control接口,该接口提供用于管理表单值、状态、可见性、要求和验证的常见方法。 使用此引用标识可用的方法和返回类型。 有关特定于控件的值类型和其他成员,请参阅 客户端 API 控件类型。

方法 Returns Description
getDisabled() boolean true如果控件处于禁用状态,则返回;否则返回 false。
getRequired() boolean 如果 true 控件是必需的,则返回;否则返回 false。
getVisible() boolean 如果 true 控件可见,则返回;否则返回 false。
getName() string 返回控件的名称。
getType() string | undefined 返回控件类型,例如 MultiSelectPicklist,或 undefined 类型不可用时。 请参阅 客户端 API 控件类型。
getLogicalName() string | undefined 返回控件的逻辑名称或 undefined 逻辑名称不可用时。
getValue() Varies 检索当前值。 返回类型取决于 控件类型。
setDisabled(isDisabled: boolean) void 设置控件的禁用状态。
setRequired(isRequired: boolean, errorMessage?: string) void 设置控件的必需状态。 有关详细信息,请参阅 setRequired 方法参数。
setVisible(isVisible: boolean) void 设置控件的可见性。
setValue(value) void 设置一个新值。 预期值类型取决于 控件类型。
addValidator(id: string, evaluationFn: function, errorMessage: string) void 在此控件实例上注册自定义客户端验证程序。 有关详细信息,请参阅 addValidator 方法参数。
removeValidator(id: string) void 通过此控件实例的 ID 删除自定义验证程序。 当 ID 不存在时,它不执行任何操作。
clearValidators() void 删除添加到此控件实例 addValidator的所有自定义验证程序。 它不会影响管理所需的验证程序 setRequired 。

setRequired 方法参数

Parameter 类型 Description
isRequired 布尔值 指定控件是否是必需的。
errorMessage 字符串(可选) 本地化的验证消息。 默认值为 <field name> is a required field.

如果 isRequired 为 true,客户端 API 将注册在提交表单时运行的验证程序。 如果为 false,则删除验证程序。 当控件在服务器上标记为必需时,不能将其设置为可选。

addValidator 方法参数

Parameter 类型 Description
id 字符串 当前控件实例的唯一标识符。 具有相同 ID 的新验证程序将替换现有的验证程序。
evaluationFn 函数 接收控件值,并在值有效时返回 true。
errorMessage 字符串 验证失败时显示在验证摘要中的消息。

重要

替换或删除验证程序时,请保留并重复使用同一 Control 对象实例。 每次访问 form.controls 和每次调用都 getControlByLogicalName 返回新的控件外观。

如果计算函数或 getValue 方法引发错误,验证将失败,浏览器控制台将记录错误。

Microsoft.PowerPages.onPagesClientApiReady(async function ($pages) {
    const form = $pages.currentPage.forms.getFormById('form_#1');
    const control = form.getControlByLogicalName('new_keyword');

    control.setRequired(true, 'This field is mandatory.');
    control.addValidator(
        'keywordCheck',
        (value) => ['alpha', 'beta', 'gamma'].includes(value),
        'Value must be one of: alpha, beta, gamma.'
    );
});