From a1d2b9ec53e141016431402d1d3c6b17a738b78f Mon Sep 17 00:00:00 2001 From: David Karlsson Date: Tue, 12 Dec 2023 09:41:30 +0100 Subject: [PATCH] Only send checkbox/radio values for remote validation if checked Doing remote validation for a checkbox value always sends the input's value whether the checkbox is checked or not and since the value is set to "true" for checkboxes the value will always be true on the server side even if you unchecked the checkbox when the remote validation was triggered. This change fixes this by only sending the value if the checkbox was checked. This is similar to what jquery-validation-unobtrusive does to handle remote checkbox validation. I also added radio buttons to the check but I don't think it will work correctly for them since there will be multiple inputs with the same name selector for radio buttons but only the first one is handled which might not be the one that is clicked. So I'm not sure what the correct approach is for remote validation of radio buttons but this at least seems to be more in line with how jquery-validation-unobtrusive handles them. --- src/index.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 29621f3..8925c69 100644 --- a/src/index.ts +++ b/src/index.ts @@ -351,7 +351,12 @@ export class MvcValidationProviders { continue; } - fields[fieldName] = fieldElement.value; + if (fieldElement instanceof HTMLInputElement && + (fieldElement.type === 'checkbox' || fieldElement.type === 'radio')) { + fields[fieldName] = fieldElement.checked ? fieldElement.value : ''; + } else { + fields[fieldName] = fieldElement.value; + } } let url: string = params['url'];