根据用户确认跳过显示表单字段 [英] Skip displaying form fields based on user confirmation

查看:76
本文介绍了根据用户确认跳过显示表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个类中有大约10个属性,基于这些属性,我有一个要求用户输入的表单.我想知道是否存在任何机制,在最初的4-5个问题之后,我问用户是否想要进一步回答,如果答复是肯定的,则询问下一组字段/问题.

I have some 10 properties in a class and based on those properties I have a form that asks for user input. I want to know if there's any mechanism that after initial 4-5 questions I ask user if he/she wants to ans more and if reply is yes then next set of fields/questions are asked.

我尝试使用SetDefine进行操作,但SetDefine的问题是它在每个字段中都调用了它,因此它要求用户对每个字段进行确认,但是我希望它仅在第一个可选字段中进行确认,并根据答案跳过全部或全部获得.

I tried doing it with SetDefine but issue with SetDefine is that its called with each field so it asks the user to confirm with each fiels but I want it to only confirm with 1st optional field and based on the answer either skip all or get all.

        public async Task StartAsync(IDialogContext context)
    {
        await context.PostAsync($"Welcome to the Order helper!");

        var orderFormDialog = FormDialog.FromForm(BuildOrderAdvanceStepSearchForm, FormOptions.PromptInStart);

        context.Call(orderFormDialog, ResumeAfterOrdersFormDialog);
    }

    private IForm<OrderSearchQuery> BuildOrderAdvanceStepSearchForm()
    {
        return new FormBuilder<OrderSearchQuery>()
            .Field(nameof(OrderSearchQuery.ItemNumber))
            .Field(nameof(OrderSearchQuery.Draft))
            .Field(nameof(OrderSearchQuery.Dispatched))
            .Field(nameof(OrderSearchQuery.InTransit))
            .Field(nameof(OrderSearchQuery.Delivered))
            //.Confirm("Do you want to search with more options?.")
            //.Field(nameof(OrderSearchQuery.AddField1))
            .Field(new FieldReflector<OrderSearchQuery>(nameof(OrderSearchQuery.AddField1))
                .SetDefine(OrderAdvanceStepConfirmation))
            .Field(new FieldReflector<OrderSearchQuery>(nameof(OrderSearchQuery.AddField2))
                .SetDefine(OrderAdvanceStepConfirmation))
            .Field(new FieldReflector<OrderSearchQuery>(nameof(OrderSearchQuery.AddField3))
                .SetDefine(OrderAdvanceStepConfirmation))
            .Field(new FieldReflector<OrderSearchQuery>(nameof(OrderSearchQuery.AddField4))
                .SetDefine(OrderAdvanceStepConfirmation))
            .Field(new FieldReflector<OrderSearchQuery>(nameof(OrderSearchQuery.AddField5))
                .SetDefine(OrderAdvanceStepConfirmation))
            .Build();
    }

    private static async Task<bool> OrderAdvanceStepConfirmation(OrderSearchQuery state, Field<OrderSearchQuery> field)
    {
        field.SetPrompt(new PromptAttribute($"Do you want to search with more options?."));
        return true;
    }

    private async Task ResumeAfterordersFormDialog(IDialogContext context, IAwaitable<OrderSearchQuery> result)
    {
        try
        {
            var searchQuery = await result;

            await context.PostAsync($"Ok. Searching for orders...");

            var count = 100;

            if (count > 1)
            {
                await context.PostAsync($"I found total of 100 orders");

                await context.PostAsync($"To get order details, you will need to provide more info...");

            }
            else
            {
                await context.PostAsync($"I found the order you were looking for...");

                await context.PostAsync($"Now I can provide you information related to Consumer Package, Multi-Pack, Shelf Tray & Unit Load for this order.");
            }

        }
        catch (FormCanceledException ex)
        {
            string reply;

            if (ex.InnerException == null)
            {
                reply = "You have canceled the operation. Quitting from the order Search";
            }
            else
            {
                reply = $"Oops! Something went wrong :( Technical Details: {ex.InnerException.Message}";
            }

            await context.PostAsync(reply);
        }
        finally
        {
            //context.Done<object>(null);
        }
    }

推荐答案

我希望它仅在第一个可选字段中进行确认,并根据答案要么全部跳过要么全部获取.

I want it to only confirm with 1st optional field and based on the answer either skip all or get all.

您可以使用 lowreferr/a>的 FieldReflector .

例如,创建一个枚举进行确认,例如:

For example create a enum for confirmation for example like this:

public enum Confirmation
{
    Yes,
    No
}

public Confirmation? Corfirm { get; set; }

然后您可以像这样构建Form:

And then you can build the Form like this:

return new FormBuilder<OrderSearchQuery>()
    .Field(nameof(ItemNumber))
    .Field(nameof(Draft))
    .Field(new FieldReflector<OrderSearchQuery>(nameof(Corfirm))
           .SetNext((value, state) =>
           {
               var selection = (Confirmation)value;
               if (selection == Confirmation.Yes)
               {
                   //go to step 1
                   return new NextStep();
               }
               else
               {
                   //skip the following steps
                   state.Stpe1 = null;
                   state.Step2 = null;
                   return new NextStep(StepDirection.Complete);
               }
           })
    )
    .Field(nameof(Stpe1))
    .Field(nameof(Step2)).Build();

这篇关于根据用户确认跳过显示表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆