My property needs to be 7, 30 or 60.
[Required]
public int FrequenciaConsulta { get; set; }
Is there something like '[Required]' that checks the values?
Create a custom validation attribute.
public class RequiredNumberAttribute : ValidationAttribute, IClientModelValidator
{
private int[] allowedNumbers;
public RequiredNumberAttribute(params int[] numbers)
{
allowedNumbers = numbers;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
int number = (int)value;
if (allowedNumbers.Contains(number))
{
return ValidationResult.Success;
}
return new ValidationResult($"Error: Number must be {string.Join(",", allowedNumbers)}");
}
Usage would be:
[RequiredNumber(7,30,60)]
public int FrequenciaConsulta { get; set; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With