0

I'm trying to catch error: An invalid form control with name is not focusable.

It occurs when I submit a form and there's a field required with display none;

I would like to get the field that is invalid and show in the screen.

I've tried to simulate submit button click like:

try {
    btn.click();
} catch (e) {
    [...]
}

But it doesn't enter on catch, even when logs show the error;

I've already tried both of codes:

window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {
    alert("Error occured: " + errorMsg);//or any message
    return false;
}

window.addEventListener("error", function (e) {
    alert("Error occurred: " + e.error.message);
    return false;
 })

But it doesn't catch the error too.

Caio Kretzer
  • 159
  • 1
  • 15
  • Does this answer your question? [An invalid form control with name='' is not focusable](https://stackoverflow.com/questions/22148080/an-invalid-form-control-with-name-is-not-focusable) – ControlAltDel Nov 23 '20 at 14:10
  • No, there is so much questions about this, but none answers what I need. They just describe the error and what's the cause. They say to remove the validator too, but that's not what I want. I want to catch the error to get the field that is not displayed on screen and display it. – Caio Kretzer Nov 23 '20 at 14:11
  • My guess is that this isn't an error that can be caught, because the source is not JavaScript It's a browser/validation error. But maybe someone else may have a solution for you. – ControlAltDel Nov 23 '20 at 14:21

1 Answers1

-1

Caio

I've stumbled across your post looking for the same answer.

Currently I'm working to implement a solution similar to this by hooking into the validation event:

HTML5 required, open collapse and focus required element if empty

I'm building in React and was able to hook to the invalid event listener by adding the following to my Input components:

<Input onInvalid={this.showRequiredFields} />

Which triggered the visibility as needed!

Hope this helps, please let us know!

J

Jon Humphrey
  • 114
  • 1
  • 4