Search code examples
javascriptkendo-uikendo-asp.net-mvckendo-validator

Kendo UI Validator - Handling inputs with identical name attributes


I currently have a form similar to the below:

<form action="/" method="post" id="myForm">
    <div class="row">
        <input type="text" name="rowValue" class="rowValue">
    </div>
    <div class="row">
        <input type="text" name="rowValue" class="rowValue">
    </div>
    <div class="row">
        <input type="text" name="rowValue" class="rowValue">
    </div>
    <div class="row">
        <input type="text" name="rowValue" class="rowValue">
    </div>
    <input type="submit" value="Submit">
</form>

A little background: JS is used to inject X amount of new "rows" into the form.

I tried using:

var myForm = $('#myForm').kendoValidator({
    /* rules/messages go here*/
}).data('kendoValidator');

myForm.validate();

I only get one error message showing up on the first input[name='rowValue'].

JS Fiddle

My suspicion is Kendo Validator needs unique name attributes to validate correctly. This is a shame, since lots of backend languages have the ability to accept identical name attributes, as they concatenate the values or convert them into an array or collection (ASP.NET).

Is there a way to have Kendo UI Validator validate form fields with identical name attributes?


Solution

  • Your suspicion is correct. You could adjust the validator for your use case like this:

    kendo.ui.Validator.prototype.validateInput = function (input) {
        input = $(input);
    
        var that = this,
            template = that._errorTemplate,
            result = that._checkValidity(input),
            valid = result.valid,
            className = ".k-invalid-msg",
            fieldName = (input.attr("name") || ""),
            lbl = input.parent().find("span" + className).hide(),
            messageText;
    
        input.removeAttr("aria-invalid");
    
        if (!valid) {
            messageText = that._extractMessage(input, result.key);
            that._errors[fieldName] = messageText;
            var messageLabel = $(template({
                message: messageText
            }));
    
            that._decorateMessageContainer(messageLabel, fieldName);
    
            if (!lbl.replaceWith(messageLabel).length) {
                messageLabel.insertAfter(input);
            }
            messageLabel.show();
    
            input.attr("aria-invalid", true);
        }  
        input.toggleClass("k-invalid", !valid);
    
        return valid;
    };
    

    Note that there are a few simplifications in this method, so it may break on certain corner cases.

    (demo)