1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-11-24 22:42:53 +01:00
Minecraft-Phantom-Panel/Server/Phantom.Server.Web.Components/Utils/FormCustomValidationAttribute.cs

23 lines
839 B
C#

using System.ComponentModel.DataAnnotations;
namespace Phantom.Server.Web.Components.Utils;
public abstract class FormCustomValidationAttribute<TModel, TValue> : ValidationAttribute {
public sealed override bool IsValid(object? value) {
return base.IsValid(value);
}
protected sealed override ValidationResult? IsValid(object? value, ValidationContext validationContext) {
if (value is not TValue typedValue) {
return new ValidationResult(null, new [] { FieldName });
}
var model = (TModel) validationContext.ObjectInstance;
var result = Validate(model, typedValue);
return result == ValidationResult.Success ? result : new ValidationResult(result?.ErrorMessage, new [] { FieldName });
}
protected abstract string FieldName { get; }
protected abstract ValidationResult? Validate(TModel model, TValue value);
}