This is a migrated thread and some comments may be shown as answers.

Remote validation always stopping submit of form

2 Answers 645 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Brian asked on 10 Oct 2017, 06:54 PM

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 Answers, 1 is accepted

Sort by
0
Brian
Top achievements
Rank 1
answered on 12 Oct 2017, 01:11 AM

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");}

 

 

0
Beginner
Top achievements
Rank 1
answered on 24 Apr 2018, 07:34 PM

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.

Tags
General Discussions
Asked by
Brian
Top achievements
Rank 1
Answers by
Brian
Top achievements
Rank 1
Beginner
Top achievements
Rank 1
Share this question
or