Reputation: 3717
With one of my forms I get the error "An invalid form control with name='collectors' is not focusable." and I do understand why, because it's hidden by some JS-lib, and I already know the workarounds for that "problem" propagated. My question is another:
Is that message part of an exception or event I could easily listen to and just do "things" instead? Or is the only thing I could do to override the submit event of the underlying form and check things myself?
I would rather avoid the latter and let browsers do the work, if they can print to the console, there may be a chance that I'm able to intercept the knowledge of the browser somehow and only do something when already known it's needed.
Seems like I'm looking for something like Is there a way in JavaScript to listen console events?, but maybe there's some special event for errors during form validation or such, which is more official and less hacky.
Upvotes: 0
Views: 779
Reputation: 3717
Looks like the answer is easier than expected: With plain jQuery you can simply bind to an "invalid" event on the inputs with "required" attribute and that seems to get always fired, regardless if the input is visible or not.
self.element.bind('invalid', function(event) { alert('collectors'); });
$('input[type="file"]').bind('invalid', function(event) { alert('file'); });
In my case "collectors" is a hidden text input and the event fires whenever it doesn't have any value and I want to submit the form. Another interesting thing is that at least Chrome seems to first focus all other, already visible required inputs without any valid value first, because if I manually focus some element in my "collectors" event handler, it seems to be ignored until the file input has a value, that is focussed by Chrome automatically.
What doesn't seem to work is using window.onerror or console.error to filter the message Chrome outputs in its dev console. Those don't get called at all, I guess the error message doesn't pass JS, but comes directly from inside the browser as an error.
Upvotes: 2