2022-11-25 18:14:33 +09:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using BTCPayServer.Abstractions.Form;
|
2022-11-25 02:42:55 +01:00
|
|
|
|
|
|
|
namespace BTCPayServer.Forms;
|
|
|
|
|
|
|
|
public interface IFormComponentProvider
|
|
|
|
{
|
2022-11-25 18:14:33 +09:00
|
|
|
string View { get; }
|
|
|
|
void Validate(Form form, Field field);
|
|
|
|
void Register(Dictionary<string, IFormComponentProvider> typeToComponentProvider);
|
2023-04-04 04:01:34 +02:00
|
|
|
string GetValue(Form form, Field field);
|
2022-11-25 18:14:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
public abstract class FormComponentProviderBase : IFormComponentProvider
|
|
|
|
{
|
|
|
|
public abstract string View { get; }
|
|
|
|
public abstract void Register(Dictionary<string, IFormComponentProvider> typeToComponentProvider);
|
2023-04-04 04:01:34 +02:00
|
|
|
public virtual string GetValue(Form form, Field field)
|
|
|
|
{
|
|
|
|
return field.Value;
|
|
|
|
}
|
|
|
|
|
2022-11-25 18:14:33 +09:00
|
|
|
public abstract void Validate(Form form, Field field);
|
|
|
|
|
|
|
|
public void ValidateField<T>(Field field) where T : ValidationAttribute, new()
|
|
|
|
{
|
|
|
|
var result = new T().GetValidationResult(field.Value, new ValidationContext(field) { DisplayName = field.Label, MemberName = field.Name });
|
|
|
|
if (result != null)
|
|
|
|
field.ValidationErrors.Add(result.ErrorMessage);
|
|
|
|
}
|
2022-11-25 02:42:55 +01:00
|
|
|
}
|