【问题标题】:Vaadin Validate date, not emptyVaadin 验证日期,不为空
【发布时间】:2017-10-10 14:56:56
【问题描述】:

我正在尝试在 Vaadin 中编写验证,但我不明白如何检查日期字段是否为空

我写了这样的东西

    @Override
    public void setConfiguration(EditorConfiguration editorConfiguration) {
    boolean required = ((DateFieldConfiguration) editorConfiguration).isRequired();
    if (required == true) {
        setRequiredIndicatorVisible(true);
        addValueChangeListener(event -> validate(event.getSource().getDefaultValidator(), event.getValue()));

    }

}

    private void validate(Validator<LocalDate> defaultValidator, LocalDate localDate) {

      binder.forField(this).withValidator(validator).asRequired("Mandatory").bind(s -> getValue(),
            (b, v) -> setValue(v));

}

我已通过文本字段验证:

字符串验证器代码

public class VaadinStringEditor extends TextField implements HasValueComponent<String> {

/**
 * 
 */
private static final long serialVersionUID = 6271513226609012483L;
private Binder<String> binder;

@PostConstruct
public void init() {
    setWidth("100%");
    binder = new Binder<>();
}

@Override
public void initDefaults() {
    setValue("");
    binder.validate();
}

@Override
public void setConfiguration(EditorConfiguration editorConfiguration) {
    Validator<String> validator = ((TextFieldConfiguration) editorConfiguration).getValidator();
    if (validator != null) {
        binder.forField(this).withValidator(validator).asRequired("Mandatory").bind(s -> getValue(),
                (b, v) -> setValue(v));

    }

我在这里验证它:

question.setEditorConfiguration(new TextFieldConfiguration(textRequiredValidator()));

验证器:

    private Validator<String> textRequiredValidator() {
    return Validator.from(v -> v != null && StringUtils.trimAllWhitespace((String) v).length() != 0,
            , "Not empty");
}

【问题讨论】:

  • 根据Vaadin docs,一旦你设置了你的字段asRequired(message)(或在添加其他验证器之后)你可以在你的Ok按钮点击监听器中调用binder.validate()(或者你那里有什么)。你在哪里卡住了?
  • 我不知道如何获取 LocalDate 并检查它是否为空 binder.forField(this).withValidator(validator).asRequired("Mandatory").bind(s -> getValue() , (b, v) -> setValue(v));你知道如何为 localDate 编写验证器
  • 您阅读文档了吗?不是你必须检查价值。 binder 将使用定义的验证器来检查值,如果它们都通过,则更新绑定 bean 上的值。
  • 是的,但它应该是实时验证。我通过一个文本字段实现了这一点。因此,如果文本字段为空,我会得到一个红色标签。我会用照片更新我的问题
  • 也许你误解了 binder 应该如何工作,或者你试图解决简单地为字段定义验证器的不可能性......我现在不能说。你将如何填充这些字段?你有一个 bean,比如 Person 带有 namebirthday 字段吗?或者一些没有在 backing bean 中组合的数据?

标签: vaadin vaadin7 vaadin8


【解决方案1】:

您应该将com.vaadin.ui.DateField 用于LocalDate 值。看看下面的例子。

示例 bean:

public class MyBean {
    private LocalDate created;

    public LocalDate getCreated() {
        return created;
    }

    public void setCreated(LocalDate created) {
        this.created = created;
    }
}

编辑器

DateField dateField = new DateField("Date selector");
binder.forField(dateField)
        .bind(MyBean::getCreated, MyBean::setCreated);

如果由于某种原因您希望使用com.vaadin.ui.TextField 来编辑日期,那么您需要像这样设置转换器:

Binder<MyBean> binder = new Binder<>();
TextField textDateField = new TextField("Date here:");
binder.forField(textDateField)
        .withNullRepresentation("")
        .withConverter(new StringToLocalDateConverter())
        .bind(MyBean::getCreated, MyBean::setCreated);

转换器实现:

public class StringToLocalDateConverter implements Converter<String, LocalDate> {
    @Override
    public Result<LocalDate> convertToModel(String userInput, ValueContext valueContext) {
        try {
            return Result.ok(LocalDate.parse(userInput));
        } catch (RuntimeException e) {
            return Result.error("Invalid value");
        }
    }

    @Override
    public String convertToPresentation(LocalDate value, ValueContext valueContext) {
        return Objects.toString(value, "");
    }
}

请注意,此转换器不使用包含在更复杂情况下应考虑的信息的 ValueContext 对象。例如,应处理用户区域设置。

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多