如何确定DateField在Vaadin 8中是否有效

时间:2017-10-06 04:13:08

标签: java vaadin vaadin8

我有:

DateField dateField = new DateField("Date");
Button submitButton = new Button("Submit");
submitButton.addClickListener(click -> handleClickListener());

handleClickListener()中的位置如果DateField中存在验证错误,我想阻止提交。我知道我可以使用Binder然后binder.validate()但是这个表单没有后备对象,我只想要一个简单的表单。我该怎么做:

if(!dateField.isValid())
   // no further processing
else
   // process

我无法在DateField中找到允许检查输入值是否有效的任何代码。显然in Vaadin 7 you could !dateField.validate() which would throw an exception但似乎不再是这种情况......

我也知道可以dateField.isEmpty()或测试null,但这不起作用,因为不需要值。换句话说,它可以是空的,或者如果你输入一个值,那么它必须是一个有效的条目。

2 个答案:

答案 0 :(得分:5)

已经多次询问过这个问题,据我所知,如果没有粘合剂,就无法添加验证器。您可以查看this answer以获取有关该功能的全面说明并发出共鸣。

还有Vaadin forumgithub的讨论,2个主要建议是使用绑定器,或者手动调用验证器的值更改侦听器。但是,后一种解决方案似乎不起作用,我怀疑这是因为只有当实际值发生变化时才会触发值更改事件,这可能在您输入无效内容时发生,但我没有花太多时间进行调查。

latest suggestion on github请求binder.noBind()方法来促进这些情况,但在实施之前,您可以使用类似于以下代码示例的内容。我也讨厌使用字段绑定值的想法,所以我选择了 no setter & no getter 概念:

public class DateFieldWithValidator extends VerticalLayout {

    public DateFieldWithValidator() {
        // date field with binder
        Binder<LocalDate> binder = new Binder<>();
        DateField dateField = new DateField("Date");
        binder.forField(dateField)
              .asRequired("Please select a date")
              .bind(No.getter(), No.setter());

        // validity status
        TextField validityField = new TextField("Status:", "N/A");
        validityField.setReadOnly(true);
        validityField.addStyleName(ValoTheme.TEXTFIELD_BORDERLESS);
        validityField.setWidth("100%");

        // submit button
        Button submitButton = new Button("Submit");
        submitButton.addClickListener(event -> {
            BinderValidationStatus<LocalDate> status = binder.validate();
            if (status.isOk()) {
                validityField.setValue("OK: " + dateField.getValue().toString());
            } else {
                validityField.setValue("KO: " + status.getValidationErrors().stream().map(ValidationResult::getErrorMessage).collect(Collectors.joining(",")));
            }
        });

        addComponents(dateField, submitButton, validityField);
    }

    // convenience empty getter and setter implementation for better readability
    public static class No {
        public static <SOURCE, TARGET> ValueProvider<SOURCE, TARGET> getter() {
            return source -> null;
        }

        public static <BEAN, FIELDVALUE> Setter<BEAN, FIELDVALUE> setter() {
            return (bean, fieldValue) -> {
                //no op
            };
        }
    }
}

结果:

date field validation with binder

稍后更新:

我一直在考虑更多,如果可以接受,如果值为null或者无效值发生解析错误,则可以禁用提交按钮。这可以使用ValueChangeListenerErrorHandler轻松实现,如下所示。

或者,您可以将异常消息保存在变量中,当您单击按钮时,检查是否有错误消息或值为null,按此顺序,因为如果输入无效日期,则字段的值将为被设置为null。

public class DateFieldWithValidator extends VerticalLayout {
    public DateFieldWithValidator() {
        DateField dateField = new DateField("Date");
        Button submitButton = new Button("Submit");
        submitButton.setEnabled(false);
        submitButton.addClickListener(event -> Notification.show("Selected date: " + dateField.getValue()));
        dateField.setRequiredIndicatorVisible(true);
        dateField.setErrorHandler(event -> submitButton.setEnabled(false));
        dateField.addValueChangeListener(event -> submitButton.setEnabled(event.getValue() != null));
        addComponents(dateField, submitButton);
    }
}

结果:

vaadin date field validation

答案 1 :(得分:0)

即使您声明不想使用支持对象,我也建议您使用No。提供的addStatusChangeListener类@Morfic在我看来非常好(我倾向于在许多验证实现中重用它)。

所以我要做的是在你的活页夹上添加一个binder.addStatusChangeListener(event -> { if (event.hasValidationErrors()) { submitButton.setEnabled(false); } else { submitButton.setEnabled(true); } }); 并启用/禁用那里的按钮:

Binder

省略了Binder对象的实例化,因为@Morfic已经在他的答案中提供了一个很好的例子。

我建议使用Validator的原因是因为Vaadin suggests to use it for validation,您不需要努力维护价值变化听众,并且您可以轻松地扩展您对新{{1}的绑定如果需要,以后再提出。也许你想要在一个点上将字段绑定到一个对象。