2022-11-25 10:14:33 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using BTCPayServer.Abstractions.Form;
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Forms;
|
|
|
|
|
|
|
|
public class FormComponentProviders
|
|
|
|
{
|
|
|
|
private readonly IEnumerable<IFormComponentProvider> _formComponentProviders;
|
|
|
|
|
2023-02-23 09:52:37 +01:00
|
|
|
public Dictionary<string, IFormComponentProvider> TypeToComponentProvider = new();
|
2022-11-25 10:14:33 +01:00
|
|
|
|
|
|
|
public FormComponentProviders(IEnumerable<IFormComponentProvider> formComponentProviders)
|
|
|
|
{
|
|
|
|
_formComponentProviders = formComponentProviders;
|
|
|
|
foreach (var prov in _formComponentProviders)
|
|
|
|
prov.Register(TypeToComponentProvider);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Validate(Form form, ModelStateDictionary modelState)
|
|
|
|
{
|
2023-04-04 07:48:29 +02:00
|
|
|
foreach (var field in form.GetAllFields())
|
2022-11-25 10:14:33 +01:00
|
|
|
{
|
2023-04-04 07:48:29 +02:00
|
|
|
if (TypeToComponentProvider.TryGetValue(field.Field.Type, out var provider))
|
2022-11-25 10:14:33 +01:00
|
|
|
{
|
2023-04-04 07:48:29 +02:00
|
|
|
provider.Validate(form, field.Field);
|
|
|
|
foreach (var err in field.Field.ValidationErrors)
|
|
|
|
modelState.TryAddModelError(field.Field.Name, err);
|
2022-11-25 10:14:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return modelState.IsValid;
|
|
|
|
}
|
|
|
|
}
|