1. Sitecore Formsのチュートリアル

チュートリアル: カスタム送信アクションの作成

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

送信ボタンは、ナビゲーション(「前へ」ボタンと「次へ」ボタン)、フォームの送信、送信アクションのトリガーに使用します。すべての送信ボタンに異なるタイプのアクションを追加できます。たとえば、「次へ」ボタンに「送信」アクションが追加されSave data、連絡先がフォームの次のページに移動するために「Next」をクリックしたときに、データがフォームデータベースに保存されるようにします。デフォルトでは、トリガーゴール、トリガーキャンペーンアクティビティ、トリガー結果、ページにリダイレクト、およびデータの保存送信アイテムを追加できます。

このチュートリアルでは、フォームの送信ステータスに関するエントリをログファイルに追加するカスタム送信アクションを作成する方法について説明します。次の方法について説明します。

  • submitアクション クラスを作成する

  • サブミットアクションアイテムを作成する

submitアクション クラスを作成する

カスタム送信アクションを作成する場合は、SubmitActionBase<TParametersData> クラスを継承するクラスを作成する必要があります。

submitアクションクラスを作成するには:

  1. LogSubmitクラスを作成し、SubmitActionBase<TParametersData>

  2. TParametersDataタイプを指定します。

  3. TryParse メソッドをオーバーライドして、空の文字列パラメーターの解析を有効にし、trueを返します。

  4. Executeメソッドをオーバーライドし、フォーム送信ステータスとフォームIDを含むメッセージをログに記録します。例えば:

    using System.Linq;
    using Sitecore.Diagnostics;
    using Sitecore.ExperienceForms.Models;
    using Sitecore.ExperienceForms.Processing;
    using Sitecore.ExperienceForms.Processing.Actions;
    using static System.FormattableString;
    
    namespace Sitecore.ExperienceForms.Samples.SubmitActions
    {
        /// <summary>
        /// Executes a submit action for logging the form submit status.
        /// </summary>
        /// <seealso cref="Sitecore.ExperienceForms.Processing.Actions.SubmitActionBase{TParametersData}" />
        public class LogSubmit : SubmitActionBase<string>
        {
            /// <summary>
            /// Initializes a new instance of the <see cref="LogSubmit"/> class.
            /// </summary>
            /// <param name="submitActionData">The submit action data.</param>
            public LogSubmit(ISubmitActionData submitActionData) : base(submitActionData)
            {
            }
    
            /// <summary>
            /// Tries to convert the specified <paramref name="value" /> to an instance of the specified target type.
            /// </summary>
            /// <param name="value">The value.</param>
            /// <param name="target">The target object.</param>
            /// <returns>
            /// true if <paramref name="value" /> was converted successfully; otherwise, false.
            /// </returns>
            protected override bool TryParse(string value, out string target)
            {
                target = string.Empty;
                return true;
            }
    
            /// <summary>
            /// Executes the action with the specified <paramref name="data" />.
            /// </summary>
            /// <param name="data">The data.</param>
            /// <param name="formSubmitContext">The form submit context.</param>
            /// <returns>
            ///   <c>true</c> if the action is executed correctly; otherwise <c>false</c>
            /// </returns>
            protected override bool Execute(string data, FormSubmitContext formSubmitContext)
            {
                Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));
    
                if (!formSubmitContext.HasErrors)
                {
                    Logger.Info(Invariant($"Form {formSubmitContext.FormId} submitted successfully."), this);
                }
                else
                {
                    Logger.Warn(Invariant($"Form {formSubmitContext.FormId} submitted with errors: {string.Join(", ", formSubmitContext.Errors.Select(t => t.ErrorMessage))}."), this);
                }
    
                return true;
            }
        }
    }
  5. ビルドとデプロイ

サブミットアクションアイテムを作成する

サブミットアクションアイテムを作成するには:

  1. /sitecore/System/Settings/Formsに移動します。

  2. Submit Actionsを右クリックし、Insertをクリックして、Insert from templateをクリックします。

  3. /System/Forms/Submit Actionテンプレートを選択し、Item Nameフィールドに名前Log Submitを入力して、Insertをクリックします。

    Insert from Template dialog
  4. 作成したアイテムに移動し、SettingsセクションのModel Typeフィールドで、値をクラス タイプ名に設定します。

  5. Error Messageフィールドに、次のようなエラー メッセージを入力します。Log Submit Failed!

  6. Appearanceセクションで、フォーム要素ペインに表示するアイコンを選択します。

「フォーム要素」パネルで、「送信アクションの追加」をクリックすると、「ログ送信」アクションを選択できるようになりました。

Select the action
この記事を改善するための提案がある場合は、 お知らせください!