Remote validation always stopping submit of form

3 posts, 0 answers
  1. Brian
    Brian avatar
    19 posts
    Member since:
    Jan 2013

    Posted 10 Oct 2017 Link to this post

    I've set up remote validation using asp mvc core and I am using the kendo validator.  I set up some javascript based on the remote validation code I found both in the samples online and some other searches and I added some code to support sending additional parameters. 

    I am using the validator to verify that a name is not already in use for a form input.  What I have now actually mostly works, if the name exists then the validator adds the text below the input that the name is already used.  The issue is that when I enter a unique name, the validation message goes away but I still can't submit the form.  Something in the validation is still stopping the submit even though it appears to be validating correctly.

    This is what I am using to set up a remote rule for the validator:

    $.validator.methods.remote = function () { /* disabled */ };
     
    $('#formName').kendoValidator({
        onfocusout: true,
        onkeyup: true,
        rules: {
            remote: function (input)
            {
                var remoteAttr = input.attr("data-val-remote-url");
                if (typeof remoteAttr === typeof undefined || remoteAttr === false)
                {
                    return true;
                }
     
                var isValid = false;
                var data = {};
     
                var additionalFieldsAttribute = input.attr('data-val-remote-additionalfields');
                var parts = additionalFieldsAttribute.split(',');
     
                for (var i = 0; i < parts.length; i++)
                {
                    var piece = parts[i].replace('*.', '');
                    data[piece] = $('#' + piece).val();
                }
     
                $.ajax({
                    url: remoteAttr,
                    mode: "abort",
                    port: "validate" + input.attr('name'),
                    dataType: "json",
                    type: input.attr("data-val-remote-type"),
                    data: data,
                    async: false,
                    success: function (response)
                    {
                        isValid = response.result;
                    }
                });
                return isValid;
            }
        },
        messages: {
            remote: function (input)
            {
                return input.data('val-remote');
            }
        }
    });

     

    The controller code that it calls (this works correctly and returns the desired result):

    public IActionResult IsSubstitutionNameAvailable(string Name, int? MergeDocumentSubstitutionTypeId)
    {
        var existing = _context.MergeDocumentSubstitutionTypes
            .Where(l => l.Name == Name)
            .FirstOrDefault();
     
        if (existing == null || (existing != null && MergeDocumentSubstitutionTypeId == existing.MergeDocumentSubstitutionTypeId))
        {
            return Json(new { result = true });
        }
        else
        {
            return Json(new { result = false });
        }
    }

     

    The viewmodel where the remote attribute is added:

    [Required]
    [Remote("IsSubstitutionNameAvailable", "MergeDocumentSubstitutionTypes", AdditionalFields = "MergeDocumentSubstitutionTypeId", ErrorMessage = "Entered name is already in use.")]
    public string Name { get; set; }

     

    Again, the controller does get called, returns the correct response and I see the correct value come back from the javascript debugger.  After entering a unique name the validation message goes away but the submit is still blocked. When I inpecty the properties of the validator in the debugger it appears there are no errors.

     

    Thanks,

    Brian

     

     

  2. Brian
    Brian avatar
    19 posts
    Member since:
    Jan 2013

    Posted 11 Oct 2017 in reply to Brian Link to this post

    Following up as I found what was happening,  I was using the kendo validator but I still had the regular jquery validator running as well and that was stopping the submit.  I removed the below line from my view and it now works correctly.

     

    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

     

     

  3. Beginner
    Beginner avatar
    1 posts
    Member since:
    Aug 2017

    Posted 24 Apr 2018 Link to this post

    Brian,

    I have similar kind of setup, when i validate form using validator.validate() the value is true i still have the same problem, it is stopping the form submitting.

Back to Top