2016-11-21 12 views
3

我已經註冊了一個方法到BizFormItemEvent.Insert.Before,該方法用於檢查用戶是否已經使用表單中提供的電子郵件地址存在,並在用戶沒有時創建用戶。用註冊到BizFormItemEvent的方法設置Kentico BizForm的錯誤消息

如果用戶確實與該電子郵件地址存在,那麼我想顯示一條錯誤消息。問題是,如果您嘗試撥打e.Cancel()或引發異常,BizForm只會顯示通用的"An error occurred when saving data. Please see event log for more details"消息,而我不知道如何更改該消息。

下面是我在做什麼至今:

using CMS.Base; 
using CMS.OnlineForms; 
using System; 

/// <summary> 
/// Partial class that allows you to register custom handler methods and classes. 
/// Adds the CustomFormHandlers attribute to the partial class. 
/// </summary> 
[CustomFormHandlers] 
public partial class CMSModuleLoader 
{ 
    /// <summary> 
    /// Custom attribute class. 
    /// </summary> 
    private class CustomFormHandlers : CMSLoaderAttribute 
    { 
     /// <summary> 
     /// Called automatically when the application starts 
     /// </summary> 
     public override void Init() 
     { 
      BizFormItemEvents.Insert.Before += FormItem_InsertBeforeHandler; 
     } 

     /// <summary> 
     /// Handles the form data when users create new records for forms 
     /// </summary> 
     private void FormItem_InsertBeforeHandler(object sender, BizFormItemEventArgs e) 
     { 
      BizFormItem formDataItem = e.Item; 

      if (formDataItem != null && formDataItem.BizFormClassName == "bizform.formname") 
      { 
       // CreateUser returns false if a duplicate email is found 
       if(!CreateUser(formDataItem)) 
       { 
        // Something needs to happen here that 
        // changes the error message of the bizform 
        // e.Cancel(); 
        throw new Exception("A user with this email address already exists."); 
       } 
      } 
     } 
    } 
} 

回答

3

我會創造一個自定義表單控件並在IsValid方法中實施驗證。示例here

1

我不知道你能做到這一點的BizFormItemEvent.Insert.Before事件。試圖實現自定義的驗證規則,該字段:

  • 它會阻止形式被保存
  • 它可以讓你指定驗證錯誤消息
相關問題