【问题标题】:Validate JavaFX form only after submission using ValidationSupport仅在使用 ValidationSupport 提交后验证 JavaFX 表单
【发布时间】:2017-05-26 23:25:56
【问题描述】:

我正在尝试使用 ValidationSupport 验证 javafx 表单,验证工作正常,但是当我访问表单时,“错误装饰”已经显示,甚至在表单提交或文本字段聚焦之前。

ValidationSupport validationSupport = new ValidationSupport();
    validationSupport.registerValidator(textField, Validator.createEmptyValidator("Text is required"));

下图显示了处于初始状态的表单示例。

如何强制装饰仅在用户提交表单或更改 TextField 值后显示?

【问题讨论】:

    标签: java forms validation javafx controlsfx


    【解决方案1】:

    按需验证控件是 ControlsFx 的问题跟踪器上的一个问题 (on-demand validation option),该问题跟踪器仍处于打开状态,因此 ControlsFx 尚不支持它。

    但是有一种方法可以抑制错误修饰:

    ValidationSupport validationSupport = new ValidationSupport();
    validationSupport.setErrorDecorationEnabled(false);
    

    稍后,当您真正想要验证时(例如在提交按钮上),您需要将其重置为默认值:

    validationSupport.setErrorDecorationEnabled(true);
    validationSupport.redecorate();
    

    这样,每次更改仍然会验证字段,但在您真正希望显示错误修饰之前不会显示它们。


    示例:

    在此示例中,我们希望仅在数字字段具有焦点时查看验证错误。

    public class Sandbox extends Application {
        @Override
        public void start(Stage primaryStage) throws Exception {
            GridPane pane = new GridPane();
            pane.add(new Label("Number field:"), 0 , 0);
            TextField numberField = new TextField("");
            pane.add(numberField, 1, 0);
            TextField textField = new TextField("");
            pane.add(new Label("Text field:"), 0, 1);
            pane.add(textField, 1, 1);
    
            ValidationSupport vs = new ValidationSupport();
            vs.setErrorDecorationEnabled(false); // we don't want errors to bother us for now.
            vs.registerValidator(numberField, Validator.createRegexValidator("must be digits only!", "\\d*", Severity.ERROR));
    
            // validate and show errors only if number field has the focus
            vs.errorDecorationEnabledProperty().bind(numberField.focusedProperty());
    
            primaryStage.setScene(new Scene(pane));
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            Application.launch(Sandbox.class);
        }
    }
    

    或者如果您只想在第一次点击提交按钮后查看验证错误:

            ...
            Button button = new Button("Submit");
            pane.add(button, 0, 2);
    
            ValidationSupport vs = new ValidationSupport();
            vs.setErrorDecorationEnabled(false); // we don't want errors to bother us for now.
            vs.registerValidator(numberField, Validator.createRegexValidator("must be digits only!", "\\d*", Severity.ERROR));
    
            
            button.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    vs.setErrorDecorationEnabled(true); // validate and show errors now!
                }
            });
            ...
    

    【讨论】:

    • 这不会做任何事情。我应该怎么做op想要的?我将启用/重新装饰绑定到一个按钮,但单击它时没有任何反应。
    • 我不知道您将errorDecorationEnabledProperty 绑定到按钮上的哪个布尔属性,但通常您应该在满足某些条件时执行setErrorDecorationEnabled(true)。我添加了两个示例。
    【解决方案2】:

    您可能想看看 ValidatorFX,它试图克服 ControlsFX 中的一些验证问题。 全面披露:我是作者 auf ValidatorFX。

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多