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