You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Power Pages中自定义Dataverse唯一性约束错误提示

自定义Power Pages中Dataverse唯一性约束的错误提示

问题背景

在Dataverse的Applications表上通过备用键实现唯一性约束,确保申请人存在「审核中」状态的申请时无法提交新申请。约束功能正常,但用户提交重复申请时,Power Pages会显示通用错误:

An unknown failure has occurred. Error ID #[fc85ffdd-298c-41a9-90f6-7b6a9005148f]

需要将该错误提示替换为友好内容:「您已提交过申请,无法创建新申请。请查看您的申请页面或联系支持人员。」 但Dataverse唯一性约束本身不支持自定义错误提示,需在Power Pages中实现覆盖。

已尝试的客户端验证代码

$(document).ready(function () {
    const defaultErrorPattern = /An unknown failure has occurred\. Error ID #\[[\w\d-]+\]/i;
    const customErrorMessage = "An error occurred while processing your application. Please try again or contact support.";

    // 检测并替换默认Power Pages错误消息
    $(".alert-danger").each(function () {
        const text = $(this).text().trim();
        if (defaultErrorPattern.test(text)) {
            $(this).hide(); // 隐藏默认错误
            displayError(customErrorMessage); // 显示自定义错误
        }
    });

    // 提交前检查重复申请
    $("form.entity-form").on("submit", function (e) {
        e.preventDefault();
        var form = this;

        var applicantId = $("#ak_contact").val() || "de8c2c6f-9e3f-f011-877a-6045bd971639";
        if (!applicantId) {
            displayError("Applicant ID not found. Please log in or contact support.");
            return;
        }

        $.ajax({
            url: "/_api/ak_applications?$filter=_ak_contact_value eq " + applicantId + " and statuscode eq 121260003",
            method: "GET",
            headers: {
                "Accept": "application/json",
                "OData-MaxVersion": "4.0",
                "OData-Version": "4.0",
                "Prefer": "odata.include-annotations=*"
            },
            success: function (data) {
                if (data.value.length > 0) {
                    displayError("You already have a submitted application and cannot create a new one. Please check your Applications page or contact support.");
                } else {
                    $("#error-message").remove();
                    form.submit();
                }
            },
            error: function () {
                displayError("Error checking for duplicates. Please try again or contact support.");
            }
        });
    });

    // 显示错误的辅助函数
    function displayError(message) {
        var errorDiv = $("#error-message");
        if (errorDiv.length === 0) {
            errorDiv = $('<div id="error-message" class="alert alert-danger" style="margin: 15px;"></div>');
            $("form.entity-form").prepend(errorDiv);
        }
        errorDiv.text(message).show();
    }
});

优化解决方案

1. 完善客户端前置验证(避免触发Dataverse约束)

原代码逻辑可行,但存在优化空间,修改后代码如下:

$(document).ready(function () {
    const duplicateAppError = "您已提交过申请,无法创建新申请。请查看您的申请页面或联系支持人员。";
    const loginRequiredError = "请先登录再提交申请。";
    const checkFailedError = "检查申请状态失败,请稍后重试或联系支持人员。";

    // 提交前拦截并检查重复申请
    $("form.entity-form").on("submit", function (e) {
        const $form = $(this);
        // 防止重复提交
        if ($form.data('isSubmitting')) return;
        $form.data('isSubmitting', true);

        e.preventDefault();
        const form = this;

        // 从Power Pages内置变量获取当前登录用户ID
        const applicantId = $("#ak_contact").val() || "{{user.id}}";
        if (!applicantId) {
            displayError(loginRequiredError);
            $form.data('isSubmitting', false);
            return;
        }

        $.ajax({
            url: "/_api/ak_applications?$filter=_ak_contact_value eq " + applicantId + " and statuscode eq 121260003",
            method: "GET",
            headers: {
                "Accept": "application/json",
                "OData-MaxVersion": "4.0",
                "OData-Version": "4.0"
            },
            success: function (data) {
                if (data.value.length > 0) {
                    displayError(duplicateAppError);
                } else {
                    $("#error-message").remove();
                    form.submit();
                }
                $form.data('isSubmitting', false);
            },
            error: function () {
                displayError(checkFailedError);
                $form.data('isSubmitting', false);
            }
        });
    });

    // 监听DOM变化,替换异步加载的通用错误
    const observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.addedNodes.length) {
                $(".alert-danger").each(function () {
                    const text = $(this).text().trim();
                    if (/An unknown failure has occurred\. Error ID #\[[\w\d-]+\]/i.test(text)) {
                        $(this).hide();
                        displayError(duplicateAppError);
                    }
                });
            }
        });
    });

    observer.observe(document.body, { childList: true, subtree: true });

    // 显示错误的辅助函数
    function displayError(message) {
        let errorDiv = $("#error-message");
        if (errorDiv.length === 0) {
            errorDiv = $('<div id="error-message" class="alert alert-danger" style="margin: 15px;"></div>');
            $("form.entity-form").prepend(errorDiv);
        }
        errorDiv.text(message).show();
    }
});

2. 服务器端错误拦截(兜底方案)

如果客户端验证被绕过(如禁用JS),可通过Power Pages后台设置拦截错误:

  • 进入Power Pages管理中心,打开「设置」>「错误页面」
  • 创建自定义错误页面,添加逻辑检测错误ID或类型,替换为友好提示
  • 或使用Web模板渲染表单,在服务器端直接处理Dataverse返回的错误信息

关键说明

  • 客户端前置验证是最优方案,能避免触发Dataverse约束,提升用户体验
  • MutationObserver用于捕捉Power Pages异步加载的错误提示,确保替换生效
  • 服务器端处理作为兜底,覆盖恶意绕过客户端验证的场景

内容的提问来源于stack exchange,提问作者Njabulo Ndlovu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 22:33:15