2017-10-10 123 views
0

我試着寫在Vaadin驗證,但我不知道如何檢查,如果日期字段爲空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)); 

} 

我已經achived一個驗證與文本字段:

enter image description here

字符串驗證碼

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"); 
} 
+0

根據[Vaadin文檔](https://vaadin.com/docs/v8/framework/datamodel/datamodel-forms.html#datamodel.forms.validation),一旦設置了字段'asRequired(message) '(或添加其他驗證器之後),您可以在_Ok_按鈕中單擊偵聽器(或您在其中的任何位置)調用'binder.validate()'。你卡在哪裏? – Morfic

+0

我不知道如何獲取LocalDate並檢查它是否爲空binder.forField(this).withValidator(validator).asRequired(「Mandatory」)。bind(s - > getValue(), (b,v) - > setValue(v));你知道如何爲localDate寫驗證器嗎 –

+0

你讀過文檔嗎?這不是你必須檢查價值。活頁夾將使用已定義的驗證器來檢查值,如果它們全部通過,則更新綁定bean上的值。 – Morfic

回答

0

您應該使用com.vaadin.ui.DateFieldLocalDate值。看看下面的例子。

實例豆:

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對象,該對象包含更復雜情況下應考慮的信息。例如,應該處理用戶區域設置。