aspnet-client-validation icon indicating copy to clipboard operation
aspnet-client-validation copied to clipboard

Manually validating a dynamically added form

Open max-maurice opened this issue 2 years ago • 8 comments

After adding a form to the DOM (with watch enabled), a call to isValid() fails, when submitting the form:

window.validationService.isValid(form, false);

The error TypeError: formInputUIDs is undefined occurs at https://github.com/haacked/aspnet-client-validation/blob/f84dcb417afd64081b91bad99e0d06e92d5b0755/src/index.ts#L770

Variables: grafik

Simplified form markup:

<form method="post" action="https://localhost:7222/item/disableitem/20/">
	<div class="validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li></ul></div>
	<button type="submit">Yes</button>
	<input name="__RequestVerificationToken" type="hidden" value="xxx">
	<button type="button">No</button>
</form>

max-maurice avatar Jul 13 '23 14:07 max-maurice

I was getting the same error but eventually realized the issue was being caused by trying to pass a wrapper element instead of the form itself to the isValid function by mistake. Maybe the library could check that the input is actually a form and throw a helpful error message if it isn't?

LiteracyFanatic avatar Jul 28 '23 04:07 LiteracyFanatic

Maybe the library could check that the input is actually a form and throw a helpful error message if it isn't?

That sounds like a good idea. Want to make a contribution?

haacked avatar Dec 12 '23 19:12 haacked

@haacked #85 seems to handle wrapped elements, but doesn't fix this issue where I passed an instance of HTMLFormElement. Could you please re-open it?

max-maurice avatar Jan 09 '24 15:01 max-maurice

formInputUIDs being undefined suggests the form didn't get scanned ahead of validation. Can you add a logger to your ValidationService and check if you see the form in question being scanned for inputs to validate?

I almost wonder if we should just auto-scan a form if there's an attempts to validate it but it's not been scanned.

dahlbyk avatar Jan 10 '24 03:01 dahlbyk

According to the logs, the form element itself didn't get scanned. The parent element surrounding the form (a Bootstrap modal) however did:

Added node <div class="modal-backdrop show">
aspnet-validation.js:1341:28

Scanning <div class="modal-backdrop show">
aspnet-validation.js:1306:20

Attribute 'class' changed from '' to 'modal-backdrop show' 
<div class="modal-backdrop show">
aspnet-validation.js:1358:28

Scanning <div class="modal-backdrop show">
aspnet-validation.js:1306:20

Added node <div class="modal show" tabindex="-1" role="dialog" aria-labelledby="activation_modal_label" style="display: block;" aria-modal="true">
aspnet-validation.js:1341:28

Scanning <div class="modal show" tabindex="-1" role="dialog" aria-labelledby="activation_modal_label" style="display: block;" aria-modal="true">
aspnet-validation.js:1306:20

Attribute 'style' changed from '' to 'display: block;' 
<div class="modal show" tabindex="-1" role="dialog" aria-labelledby="activation_modal_label" style="display: block;" aria-modal="true">
aspnet-validation.js:1358:28

Scanning <div class="modal show" tabindex="-1" role="dialog" aria-labelledby="activation_modal_label" style="display: block;" aria-modal="true">
aspnet-validation.js:1306:20

Attribute 'aria-hidden' changed from '' to '' 
<div class="modal show" tabindex="-1" role="dialog" aria-labelledby="activation_modal_label" style="display: block;" aria-modal="true">
aspnet-validation.js:1358:28

Attribute 'aria-modal' changed from '' to 'true' 
<div class="modal show" tabindex="-1" role="dialog" aria-labelledby="activation_modal_label" style="display: block;" aria-modal="true">
aspnet-validation.js:1358:28

Scanning <div class="modal show" tabindex="-1" role="dialog" aria-labelledby="activation_modal_label" style="display: block;" aria-modal="true">
aspnet-validation.js:1306:20

Attribute 'role' changed from '' to 'dialog' 
<div class="modal show" tabindex="-1" role="dialog" aria-labelledby="activation_modal_label" style="display: block;" aria-modal="true">
aspnet-validation.js:1358:28

Scanning <div class="modal show" tabindex="-1" role="dialog" aria-labelledby="activation_modal_label" style="display: block;" aria-modal="true">
aspnet-validation.js:1306:20

Attribute 'class' changed from '' to 'modal show' 
<div class="modal show" tabindex="-1" role="dialog" aria-labelledby="activation_modal_label" style="display: block;" aria-modal="true">
aspnet-validation.js:1358:28

Scanning <div class="modal show" tabindex="-1" role="dialog" aria-labelledby="activation_modal_label" style="display: block;" aria-modal="true">
aspnet-validation.js:1306:20

Added node #text "    "
aspnet-validation.js:1341:28

Added node <p>
aspnet-validation.js:1341:28

Scanning <p>
aspnet-validation.js:1306:20

Added node #text "\n    "
aspnet-validation.js:1341:28

Added node <table class="table table-striped">
aspnet-validation.js:1341:28

Scanning <table class="table table-striped">
aspnet-validation.js:1306:20

Added node #text "\n"
aspnet-validation.js:1341:28

Added node <p>
aspnet-validation.js:1341:28

Scanning <p>
aspnet-validation.js:1306:20

Removed node 
<div class="spinner-border m-auto d-flex" role="status">
aspnet-validation.js:1348:28

Removing 
<div class="spinner-border m-auto d-flex" role="status">

max-maurice avatar Jan 10 '24 10:01 max-maurice

My form contains only hidden inputs. After adding a dummy text input, it worked.

Maybe isValid() could return true for automatically added forms without validatable inputs?

max-maurice avatar Jan 10 '24 14:01 max-maurice

Only hidden inputs never would have occurred to me! Returning true is the correct behavior, it's just a question of what the right implementation is. I can peek at it if @haacked doesn't get to it first.

dahlbyk avatar Jan 10 '24 23:01 dahlbyk

All yours! But please review my other PR. 😂

haacked avatar Jan 11 '24 00:01 haacked

@dahlbyk you still planning to look at this?

haacked avatar Mar 19 '24 19:03 haacked

I haven't had a chance yet. up-for-grabs

dahlbyk avatar Mar 20 '24 06:03 dahlbyk

Give #105 a try?

dahlbyk avatar Mar 27 '24 12:03 dahlbyk

Fix shipped in https://www.npmjs.com/package/aspnet-client-validation/v/0.9.6, please let us know if you still see an issue.

dahlbyk avatar Mar 27 '24 18:03 dahlbyk