在formFlow中添加验证步骤-检查是否有浇头 [英] Adding verification step in formFlow - Check if topping is in stock
本文介绍了在formFlow中添加验证步骤-检查是否有浇头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用这里
该示例使用formFlow来帮助用户选择他们想要的三明治上的浇头.
The example uses formFlow to help the user pick out the toppings they want on their sandwich.
我正在尝试添加一个验证步骤,以检查他们添加的每种浇头是否都在库存中,以及是否未发送道歉消息并提示用户输入其他浇头.下面是一个代码示例:
I'm trying to add a verification step that checks if each of the toppings they add is in stock and if it isn't send an apology message and prompt the user to enter a different topping instead. A code example is seen below:
public static IForm<SandwichOrder> BuildForm()
{
return new FormBuilder<SandwichOrder>()
.Message("Welcome to the sandwich order bot!")
.Field(nameof(Sandwich))
.Field(nameof(Length))
.Field(nameof(Bread))
.Field(nameof(Cheese))
.Field(nameof(Topping),
validate: async (state, value) =>
{
foreach(var t in Topping)
{
if (!isToppinginStock)
{
// Apology message
//Code to ask topping question again
}
}
})
.Message("For sandwich toppings you have selected {Toppings}.")
.Build();
}
如果有人能帮助我或指出正确的方向,我将不胜感激.
If anyone can help or point me in the right direction I would really appreciate it.
推荐答案
类似以下内容应适用于您的场景:
Something like the following should work for your scenerio:
.Field(nameof(Toppings),
validate: async (state, value) =>
{
var values = ((List<object>)value).OfType<ToppingOptions>();
var notInStock = GetOutOfStockToppings(values);
if(notInStock.Any())
return new ValidateResult { IsValid = false, Value = null, Feedback = $"These are not in stock: {string.Join(",", notInStock.ToArray())} Please choose again." };
return new ValidateResult { IsValid = true, Value = values};
})
static IEnumerable<ToppingOptions> NotInStock = new[] { ToppingOptions.Lettuce, ToppingOptions.Pickles };
private static IEnumerable<ToppingOptions> GetOutOfStockToppings(IEnumerable<ToppingOptions> toppings)
{
List<ToppingOptions> result = new List<ToppingOptions>();
foreach(var topping in toppings)
{
if (NotInStock.Contains(topping))
result.Add(topping);
}
return result;
}
这篇关于在formFlow中添加验证步骤-检查是否有浇头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文