Saturday, 27 February 2021

MVC - Custom Validation - Error not displayed

 Watch this example on YouTube

To fix it replace

  public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule
            {
                ValidationType = "validatecheckbox"
            };
            rule.ValidationParameters.Add("fields", string.Join(",", _fields));
            yield return rule;
        }

with

 public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule
            {
                ValidationType = "validatecheckbox",
                ErrorMessage =  metadata.GetDisplayName() + " is mandatory"

            };
            rule.ValidationParameters.Add("fields", string.Join(",", _fields));
            yield return rule;
        }

No comments:

Post a Comment