Unobtrusive validation of collection

前端 未结 1 606
轻奢々
轻奢々 2020-12-22 05:10

My model contains a collection:

public ICollection ChildAges { get; set; }

This is a dynamic list of ages that can be added to,

相关标签:
1条回答
  • 2020-12-22 06:10

    I created a new custom IClientValidatable attribute:

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
        public class MultipleRequiredValuesAttribute : RequiredAttribute, IClientValidatable
        {
            #region IClientValidatable Members
    
            public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
            {
                var clientValidationRule = new ModelClientValidationRule()
                {
                    ErrorMessage = base.ErrorMessage,
                    ValidationType = "multiplerequiredvalues"
                };
    
                return new[] { clientValidationRule };
            }
    
            #endregion
        }
    

    and applied this to my model:

    [DisplayName("Ages(s)")]
            [MultipleRequiredValues(ErrorMessage = "You must provide ages for all children in all rooms")]
            public ICollection<int> ChildAges { get; set; }
    

    I can then add the JQuery side:

    (function ($) {
    
        $.validator.addMethod('multiplerequiredvalues', function (value, element) {
            if ($(element).is(':visible')) {
                var returnVal = true;
                var name = $(element).attr('name');
                var elements;
                if ($(element).is('input')) {
                    elements= $('input[name='+name+']');
                }
                else
                {
                    elements= $('select[name='+name+']');
                }
                elements.each(function() {
                    if ($(this).is(':visible'))
                    {
                        returnVal = $(this).val() != "" && $(this).val() != null;
                    }
                });
                return returnVal;
            }
            else {
                return true;
            }
        });
        $.validator.unobtrusive.adapters.addBool("multiplerequiredvalues");
    } (jQuery));
    

    Note this also returns true if the element isn't visible

    0 讨论(0)
提交回复
热议问题