Guillaume F.
Guillaume F.

Reputation: 1130

JavaFX WebView: how to check if form is valid?

Is there a way to tell if a form is valid from Java?

Basically, a method that returns true if all input fields constrains are satisfied and false otherwise.

Strangely enough, org.w3c.dom.html.HTMLFormElement doesn't actually have a checkValidity() method.

EDIT:

Even more strangely, the implementation com.sun.webkit.dom.HTMLFormElementImpl does support the method checkValidity(). Unfortunately, the package com.sun.webkit is not accessible directly and thus the method is unavailable.

Upvotes: 1

Views: 227

Answers (2)

Guillaume F.
Guillaume F.

Reputation: 1130

Cast the form to JSObject

Most HTML Java elements, including HTMLFormElement, can be directly cast to the JavaScript object JSObject. It is then trivial to validate the form.

Example:

JSObject jsObject = (JSObject) form;
boolean valid = (boolean) jsObject.call("checkValidity");

Upvotes: 1

VGR
VGR

Reputation: 44404

DOM objects like HTMLFormElement only model structure. They are not capable of executing JavaScript.

However, WebEngine itself does have a JavaScript interpreter, which you can invoke using the executeScript method:

boolean valid = (Boolean) webView.getEngine().executeScript(
    "document.forms[0].checkValidity();");

The checkValidity() method is documented here and here.

Upvotes: 4

Related Questions