1

I'm trying to work out how to create a MinValue attribute that contains the necessary Client validation rules to work client side - I have this which is based on other attributes I have created but I'm not sure how to add the _minValue to the GetClientValidationRules method:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class MinValueAttribute : ValidationAttribute, IClientValidatable
{
    private readonly int _minValue;

    public MinValueAttribute(int minValue)
    {
        this._minValue = minValue;
    }

    public override bool IsValid(object value)
    {
        return (int)value >= this._minValue;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        yield return new ModelClientValidationRule
        {
            ErrorMessage = this.ErrorMessage,
            ValidationType = "min"
        };
    }
}

I'm not looking for an Int32.MaxValue Range Rule hack either, unless of course there is absolutely no way I can do it using something similar to the above code.

Rob
  • 10,004
  • 5
  • 61
  • 91

0 Answers0