2017-10-10 1 views
0

イムVaadinで検証を書き込もうとしたが、私は日付フィールドが空であるかどうかを確認する方法を理解していないVaadin検証日付ではなく、空の

私はAをachivedているこの

@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)); 

} 

のようなものを書きましたテキストフィールドでの検証:

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)); 

    } 

とI有効なここ:

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 docs](https://vaadin.com/docs/v8/framework/datamodel/datamodel-forms.html#datamodel.forms.validation)によると、フィールドに 'asRequired(message) '(または他のバリデータを追加した後)、あなたの_Ok_ボタンクリックリスナー(またはそこにあるもの)の中で' binder.validate() 'を呼び出すことができます。どこで立ち往生しましたか? – Morfic

+0

LocalDateを取得し、バインダーが空であるかどうかを確認する方法がわかりませんバインダー.forField(this).withValidator(validator).asRequired( "Mandatory")バインド(s - > getValue()、 (b、v) - > setValue(v)); localDateのバリデーターを書く方法を知っています –

+0

ドキュメントを読んだことがありますか?価値をチェックしなければならないのはあなたのことではありません。バインダは、定義されたバリデータを使用して値をチェックし、すべてが合格した場合、バインドされたBeanの値を更新します。 – Morfic

答えて

0

あなたはLocalDateの値についてcom.vaadin.ui.DateFieldを使用する必要があります。次の例を見てください。

例豆:

public class MyBean { 
    private LocalDate created; 

    public LocalDate getCreated() { 
     return created; 
    } 

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

エディタ何らかの理由で編集する日付のcom.vaadin.ui.TextFieldを持ちたい場合、あなたはこのようにコンバータを設定する必要が

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

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オブジェクトを使用しないことに注意してください。たとえば、ユーザーロケールを処理する必要があります。