如何为文本字段组合多个ControlsFX验证器?

时间:2018-10-16 14:07:38

标签: validation javafx controlsfx

我正在尝试从文本字段验证以下内容: 1.它不为空(有一个值) 2.是一个数字(不是其他字符) 3.是否具有一定的最小和最大(范围)

为此,我分别使用一个空的验证器,一个正则表达式验证器和一个谓词验证器,并将它们组合在一起。但是,它似乎仅利用列表中的最后一个验证器,而忽略了其他验证器。

代码如下:

private final Spinner<Integer> createSpinner(ValueRange range, int initialValue){
        int min = (int) range.getMinimum();
        int max = (int) range.getMaximum();

        //TODO add validation in here
        Spinner<Integer> spinner = new Spinner<Integer>();
        spinner.setPrefSize(30, 30);
        spinner.getStyleClass().add(Spinner.STYLE_CLASS_SPLIT_ARROWS_VERTICAL);
        spinner.setEditable(true);

        SpinnerValueFactory.IntegerSpinnerValueFactory valueFactory = new SpinnerValueFactory.IntegerSpinnerValueFactory(min, max, initialValue);
        valueFactory.setConverter(new TimeIntegerStringConverter());
        valueFactory.setWrapAround(true);
        spinner.setValueFactory(valueFactory);

        //validation
        TextField editor = spinner.getEditor();
        validationSupport.registerValidator(editor, false, Validator.<String>combine(
                Validator.createEmptyValidator("Field cannot be empty",Severity.WARNING),
                checkInRangeValidator(range),
                checkIntegerValidator()
                ));

        return spinner;
    }

private static Validator<String> checkInRangeValidator(ValueRange range) {
        String message = String.format("Input value must be between [%d,%d]", (int) range.getMinimum(), (int) range.getMaximum());
        return Validator.<String>createPredicateValidator(
                s ->   range.isValidIntValue(Integer.valueOf(s)),
                message,
                Severity.ERROR
                );
    }

    private static Validator<String> checkIntegerValidator(){
        String message = "Input must be a number";
        return Validator.createRegexValidator(message, "\\d+", Severity.ERROR);

    }

我不确定验证器为什么不合并。如果我将它们分别添加到Combine函数中,则每个验证器都可以工作。但是他们在一起却没有。

1 个答案:

答案 0 :(得分:0)

validationSupport.registerValidator(editor, false, Validator.<String>combine(
                emptyValidator,
                twoDigitValidator,
                new RangeValidator(range)
                ));


private final Validator<String> emptyValidator = (control,value) ->
             ValidationResult.fromErrorIf(control, "Value cannot be empty", StringExtended.isEmptyOrNullOrWhitespace(value));


    private final Validator<String> twoDigitValidator = (control,value) -> {
            return ValidationResult.fromErrorIf(control, "Value must consist of 2 digits", !value.matches("\\d{2}"));

    };

    private class RangeValidator implements Validator<String>{

        private ValueRange range;

        RangeValidator(ValueRange range){
            this.range = range;
        }

        @Override
        public ValidationResult apply(Control control, String value) {
            String message = String.format("Input value must be between [%d,%d]", (int) range.getMinimum(), (int) range.getMaximum());
            return ValidationResult.fromErrorIf(control, message,  !range.isValidIntValue(Integer.valueOf(value)));
        }

    }