我的Formflow对话框包含一个已应用自定义验证的字段...
var form = builder
.Field(new FieldReflector<CarValuationDialog>(nameof(UserName))
.SetActive(state => string.IsNullOrEmpty(state.UserName)))
.Field(new FieldReflector<CarValuationDialog>(nameof(ValuationOption))
.SetPrompt(new PromptAttribute($"Hello {{UserName}}.<br /><br />Are you looking to get an price estimate for a car you’re selling, or for a car you’re looking to buy? {{||}}")))
.Field(new FieldReflector<CarValuationDialog>(nameof(RegistrationNumber))
.SetDefine(RegistrationNumberDefinitionMethod))
.Field(new FieldReflector<CarValuationDialog>(nameof(Mileage))
.SetValidate(async (state, value) =>
{
var result = new ValidateResult { IsValid = true, Value = value };
if (int.TryParse(value.ToString(), out int mileage))
{
result.IsValid = true;
}
else
{
result.Feedback = "That isn't valid number. Can you enter it again please?";
result.IsValid = false;
}
return await Task.FromResult(result);
}))
.Field(nameof(PreviousOwnerOption),
active: carValuationDialog => carValuationDialog.ValuationOption == ValuationOptions.LookingToSell)
.Field(nameof(ServiceHistoryOption),
active: carValuationDialog => carValuationDialog.ValuationOption == ValuationOptions.LookingToSell)
.Confirm(Confirmation)
.OnCompletion(GetValuationAndDisplaySummaryToUser);
当执行SetValidate()
且给定的里程值不是int
时,将执行result.Feedback = ...
行。但是,没有看到消息“那不是有效的里程值。请您再输入一次?”我看到了...
完全公开,我确实在对话框类中声明了以下属性
[Template(TemplateUsage.NotUnderstood, "Sorry, I don't understand '{0}'.")]
但是我想在这种情况下会向用户显示result.Feedback
文本吗?如果用户输入无效的里程,则不使用Template[]
会显示以下文本...
“ d”不是汽车行驶里程选项。
哪个看上去很垃圾,这就是为什么我使用了Template[]
覆盖。
答案 0 :(得分:4)
编辑 我能够通过将模板应用于字段本身进行验证来实现此目的。这应该是您要查找的确切行为。请注意,我没有进行广泛的测试,我只是想让总体思路起作用。这是我使用的代码:
[Serializable]
public class SandwichOrder
{
//public PreviousOwnerOptions Options;
[Template(TemplateUsage.NotUnderstood, "Sorry, I don't understand '{0}'.")]
[Prompt("What is the {&}?")]
public int Mileage;
[Template(TemplateUsage.NotUnderstood, "Sorry, This is not valid '{0}'.")]
[Prompt("How many {&}?")]
public int Days;
public static IForm<SandwichOrder> BuildForm()
{
var form = new FormBuilder<SandwichOrder>()
.Field(new FieldReflector<SandwichOrder>(nameof(Mileage))
.SetValidate(async (state, value) =>
{
var result = new ValidateResult {IsValid = true, Value = value};
if (int.TryParse(value.ToString(), out int mileage))
{
result.IsValid = true;
}
else
{
result.IsValid = false;
}
return await Task.FromResult(result);
}))
.Field(new FieldReflector<SandwichOrder>(nameof(Days))
.SetValidate(async (state, value) =>
{
var result = new ValidateResult { IsValid = true, Value = value };
if (int.TryParse(value.ToString(), out int days))
{
result.IsValid = true;
}
else
{
result.IsValid = false;
}
return await Task.FromResult(result);
}));
return form.Build();
}
}
这产生了这个结果: