How to prevent a question to be shown in the Confirmation prompt?

49 Views Asked by At

I dont want one question to be showed in the final confirmartion

enter image description here

1

There are 1 best solutions below

2
Kyle Delaney On BEST ANSWER

I can think of three ways to do this. As an example, I'll use a form with FirstName and LastName and MiddleName, with MiddleName being the field you want to hide.

Option 1

You could customize your confirm step to explicitly list every field except one:

public static IForm<MyClass> BuildForm()
{
    var fieldNames = new[] { nameof(FirstName), nameof(MiddleName), nameof(LastName) };
    var confirmation = new StringBuilder("Is this your selection?");
    var formBuilder = new FormBuilder<MyClass>();

    foreach (var name in fieldNames)
    {
        // Add the field to the form
        formBuilder.Field(name);

        // Add the field to the confirmation prompt unless it's MiddleName
        if (name != nameof(MiddleName))
        {
            confirmation.AppendLine().Append($"* {{&{name}}}: {{{name}}}");
        }
    }

    formBuilder.Confirm(new PromptAttribute(confirmation.ToString()) {
        FieldCase = CaseNormalization.None
    });

    return formBuilder.Build();
}

That may look a bit complicated, but it's just a more dynamic way of saying this:

public static IForm<MyClass> BuildForm()
{
    var formBuilder = new FormBuilder<MyClass>()
        .Field(nameof(FirstName))
        .Field(nameof(MiddleName))
        .Field(nameof(LastName))
        .Confirm(new PromptAttribute("Is this your selection?\n* {&FirstName}: {FirstName}\n* {&LastName}: {LastName}") { FieldCase = CaseNormalization.None })
        ;

    return formBuilder.Build();
}

Note that this uses pattern language.

Option 2

You could make a field inactive during the confirmation step. This is a bit tricky because you'd need to make sure the field is active during every other step. In my example this is accomplished by setting a NextDelegate for each field. MiddleName should be marked as optional in this case.

[Optional]
public string MiddleName { get; set; }

public static IForm<MyClass> BuildForm()
{
    NextDelegate<MyClass> next = (value, state) =>
    {
        // Make sure MiddleName is active during most steps
        state._isMiddleNameActive = true;
        return new NextStep();
    };

    var formBuilder = new FormBuilder<MyClass>()
        .Field(new FieldReflector<MyClass>(nameof(FirstName)).SetNext(next))
        .Field(new FieldReflector<MyClass>(nameof(MiddleName)).SetNext(next)
            .SetActive(state => state._isMiddleNameActive))
        .Field(new FieldReflector<MyClass>(nameof(LastName)).SetNext(next))
        ;

    formBuilder.Confirm(async state =>
        {
            // Make sure MiddleName is inactive during the confirmation step
            state._isMiddleNameActive = false;

            // Return the default confirmation prompt
            return new PromptAttribute(
                formBuilder.Configuration.Template(TemplateUsage.Confirmation));
        });

    return formBuilder.Build();
}

// This private field isn't included in the form but is accessible via the form's state
private bool _isMiddleNameActive;

Option 3

You could use a custom prompter. This is a bit advanced but it gives you the most control.

public static IForm<MyClass> BuildForm()
{
    var formBuilder = new FormBuilder<MyClass>()
        .Prompter(PromptAsync)
        ;

    return formBuilder.Build();
}

/// <summary>
/// Here is the method we're using for the PromptAsyncDelgate.
/// </summary>
private static async Task<FormPrompt> PromptAsync(IDialogContext context,
    FormPrompt prompt, MyClass state, IField<MyClass> field)
{
    var preamble = context.MakeMessage();
    var promptMessage = context.MakeMessage();

    // Check to see if the form is on the confirmation step
    if (field.Name.StartsWith("confirmation"))
    {
        // If it's on the confirmation step,
        // we want to remove the MiddleName line from the text
        var lines = prompt.Prompt.Split('\n').ToList();
        var middleNameField = field.Form.Fields.Field(nameof(MiddleName));
        var format = new Prompter<MyClass>(
            middleNameField.Template(TemplateUsage.StatusFormat), field.Form, null);
        var middleNameLine = "* " + format.Prompt(state, middleNameField).Prompt;
        lines.RemoveAll(line => line.StartsWith(middleNameLine));
        prompt.Prompt = string.Join("\n", lines);
    }

    if (prompt.GenerateMessages(preamble, promptMessage))
    {
        await context.PostAsync(preamble);
    }

    await context.PostAsync(promptMessage);

    return prompt;
}