Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Firefox post-validation submit handling #62

Merged
merged 6 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DemoWeb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
</PropertyGroup>

<Target Name="CopyFiles">
<Exec Command="npm run build" />

<ItemGroup>
<!-- Because this ItemGroup is inside the target, this will enumerate
all files just before calling Copy. If the ItemGroup were outside
Expand Down
16 changes: 14 additions & 2 deletions Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,21 @@
</form>
</fieldset>

@section scripts {
<script>
document.addEventListener('DOMContentLoaded', function () {
console.log('Listening to Form 4')
document.getElementById('prevent-default').addEventListener('submit', function (e) {
e.preventDefault();
let data = new FormData(e.currentTarget);
alert(new URLSearchParams(data));
});
});
</script>
}
<fieldset>
<legend>Form 4</legend>
<form method="post">
<legend>Form 4 (<code>preventDefault()</code>)</legend>
<form method="post" id="prevent-default">
<div class="form-field">
<label asp-for="Input.SomeRequiredField"></label>
<input asp-for="Input.SomeRequiredField"/>
Expand Down
3 changes: 2 additions & 1 deletion Pages/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
<script src="/dist/aspnet-validation.js"></script>
<script>
const service = new aspnetValidation.ValidationService(console);
service.bootstrap();
service.bootstrap(console.log);
</script>
@RenderSection("scripts", required: false)

</body>
</html>
34 changes: 23 additions & 11 deletions dist/aspnet-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -859,18 +859,30 @@ var ValidationService = /** @class */ (function () {
}
validating = true;
_this.logger.log('Validating', form);
validate.then(function (success) {
_this.logger.log('Validated (success = %s)', success, form);
if (callback) {
callback(success);
return;
}
var validationEvent = new CustomEvent('validation', {
detail: { valid: success }
validate.then(function (success) { return __awaiter(_this, void 0, void 0, function () {
var validationEvent;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.logger.log('Validated (success = %s)', success, form);
if (callback) {
callback(success);
return [2 /*return*/];
}
validationEvent = new CustomEvent('validation', {
detail: { valid: success }
});
form.dispatchEvent(validationEvent);
// Firefox fix: redispatch 'submit' after finished handling this event
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 0); })];
case 1:
// Firefox fix: redispatch 'submit' after finished handling this event
_a.sent();
this.handleValidated(form, success, e);
return [2 /*return*/];
}
});
form.dispatchEvent(validationEvent);
_this.handleValidated(form, success, e);
}).catch(function (error) {
}); }).catch(function (error) {
_this.logger.log('Validation error', error);
}).finally(function () {
validating = false;
Expand Down
2 changes: 1 addition & 1 deletion dist/aspnet-validation.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/aspnet-validation.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aspnet-client-validation",
"version": "0.8.13",
"version": "0.8.14",
"description": "Enables ASP.NET MVC client-side validation, without jQuery!",
"main": "dist/aspnet-validation.js",
"style": "dist/aspnet-validation.css",
Expand Down
9 changes: 3 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,6 @@ export class ValidationService {
* @returns
*/
isFieldValid = (field: HTMLElement, prevalidate: boolean = true, callback?: ValidatedCallback) => {

if (prevalidate) {
let form = field.closest("form");
if (form != null) {
Expand Down Expand Up @@ -846,7 +845,7 @@ export class ValidationService {
validating = true;
this.logger.log('Validating', form);

validate.then(success => {
validate.then(async success => {
this.logger.log('Validated (success = %s)', success, form);
if (callback) {
callback(success);
Expand All @@ -859,6 +858,8 @@ export class ValidationService {
});
form.dispatchEvent(validationEvent);

// Firefox fix: redispatch 'submit' after finished handling this event
await new Promise(resolve => setTimeout(resolve, 0));
this.handleValidated(form, success, e);
}).catch(error => {
this.logger.log('Validation error', error);
Expand Down Expand Up @@ -963,7 +964,6 @@ export class ValidationService {
let renderedMessages = [];
let ul = document.createElement('ul');
for (let key in this.summary) {

// It could be that the message we are rendering belongs to one of a fieldset of multiple inputs that's not selected,
// even if another one in the fieldset is. In that case the fieldset is valid, and we shouldn't render the message.
const matchingElement = this.elementByUID[key];
Expand Down Expand Up @@ -1054,7 +1054,6 @@ export class ValidationService {
this.ValidationInputValidCssClassName);

if (input.form) {

// Adding an error to one input should also add it to others with the same name (i.e. for radio button and checkbox lists).
const inputs = input.form.querySelectorAll(`input[name="${input.name}"]`);
for (let i = 0; i < inputs.length; i++) {
Expand Down Expand Up @@ -1112,7 +1111,6 @@ export class ValidationService {
*/
createValidator(input: ValidatableElement, directives: ValidationDirective) {
return async () => {

// only validate visible fields
if (!this.isHidden(input)) {
for (let key in directives) {
Expand Down Expand Up @@ -1153,7 +1151,6 @@ export class ValidationService {

this.removeError(input);
return true;

};
}

Expand Down