Question

I have used the above custom attribute from the link (MVC3 unobtrusive validation group of inputs), but made little modification to it. Instead of string as property type, I am checking for List which is what I needed.For me validation is working fine. But errormessage is not shown. When I debugged I figured out that the error message set in data annotation of the property to be validated, is coming as null in the custom validators constructor.I tried both hard coded error message and error message from resource bundle.

Please let me know how to solve this issue.

My code for reference.

property where below attribute is used

[AtLeastOneRequired("List1", "List2", ErrorMessage = "Test Error Message", ErrorMessageResourceType = typeof(Motorola.MWM.Web.Resources.MWMResource), ErrorMessageResourceName = "ERROR_MSG_USER_GROUP")]
public List<string> List1 { get; set; }
public List<string> List2 { get; set; }

    public class AtLeastOneRequiredAttribute : ValidationAttribute, IClientValidatable
    {


     private readonly string[] _properties;
        public AtLeastOneRequiredAttribute(params string[] properties)
        {
            _properties = properties;//properties are populated
            Console.WriteLine(ErrorMessage);//but error message is null
        }

            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                if (_properties == null || _properties.Length < 1)
                {
                    return null;
                }
                foreach (var property in _properties)
                {
                    var propertyInfo = validationContext.ObjectType.GetProperty(property);
                    if (propertyInfo == null)
                    {
                        return new ValidationResult(string.Format("unknown property {0}", property));
                    }

                    var propertyValue = propertyInfo.GetValue(validationContext.ObjectInstance, null);
                    if (propertyInfo.GetType().IsGenericType && (propertyValue as List<string>) != null && (propertyValue as List<string>).Count > 0)
                    {
                        return null;
                    }

                }

                return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
            }

            public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
            {
                var rule = new ModelClientValidationRule
                {
                    ErrorMessage = ErrorMessage,
                    ValidationType = "atleastonerequired"
                };
                rule.ValidationParameters["properties"] = string.Join(",", _properties);

                yield return rule;
            }
    }

Thanks In Advance, Murali

Was it helpful?

Solution

I couldn't figure out Why ErrorMessage was coming null. But fortunately it started working now. Now both hard coded error message and error message from resource bundle also works.

But there is one change in the above code for error message from resource bundle. Instead of ErrorMessage property user ErrorMessageString which will be populated by MVC if the message is to be fetched from resource bundle.

[AtLeastOneRequired("List1", "List2", ErrorMessageResourceType = typeof(Motorola.MWM.Web.Resources.MWMResource),ErrorMessageResourceName = "ERROR_MSG_KEY")]
public List<string> List1 { get; set; }
public List<string> List2 { get; set; }


public override IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule
            {
                ErrorMessage = ErrorMessageString(Instead of ErrorMessage - which should be used only for hardcoded error messages),
                ValidationType = "atleastonerequiredlist"
            };
            rule.ValidationParameters["properties"] = string.Join(",", _properties);

            yield return rule;
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top