使用Binder进行表单验证

时间:2017-10-19 07:20:18

标签: java validation vaadin vaadin7 vaadin8

如何通过在Vaadin 8中发送所有表单数据来完成验证?不幸的是,我不理解活页夹的概念:(我写了一个字段验证,但现在是什么?它有效。我的用户看到我要求他填写一个字段,但是有什么简单的方法来验证我的所有表格?我怎么能“告诉”到我的保存按钮我的表格有效吗?

enter image description here

在我的编辑器中我定义了验证器

@SpringComponent
@PrototypeScope
public class VaadinStringEditor extends TextField implements HasValueComponent<String> {

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

    @Override
    public void initDefaults() {
        setValue("");
        status = 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));

        } 

    public BinderValidationStatus<String> getStatus() {
    return status;
    }

    public void setStatus(BinderValidationStatus<String> status) {
    this.status = status;
    }

    public boolean validate() {
     BinderValidationStatus<String> status = binder.validate();
     return status.isOk();
    }

    }

}

我还添加了TextEditorConfiguration:

public class TextFieldConfiguration implements EditorConfiguration {

    private Validator<String> validator;
    private int validated;

    public TextFieldConfiguration(Validator<String> validator) {
        this.validator = validator;

    }
    public TextFieldConfiguration() {
        this.validator = null;
    }
    public Validator<String> getValidator() {
        return validator;

    }
    public int getValidated() {
        return validated;
    }
    public void setValidated(int validated) {
        this.validated = validated;
    }
}

在我的情况下,有很多编辑器,如DateEditor等。 UI Valtidation效果很好。由于一个月我找不到如何将其连接到提交按钮以防止发送表单的方法。

在表单类中,我定义了所有问题 例如:

question = new AseQuestion(AseQuestionId.DATE_OF_NOTIFICATION, EditorType.DATE_EDITOR);
question.setDescription(
        "When it happend?");
question.setEditorConfiguration(new DateFieldConfiguration(dateRequiredValidator(), dateNotAllowedValidator()));
return question;
question = new AseQuestion(AseQuestionId.QUESTION2, EditorType.STRING_EDITOR);   
question.setDescription("
            "Write something");
private Validator<String> textRequiredValidator() {
    return Validator.from(v -> v != null && StringUtils.trimAllWhitespace((String) v).length() != 0,
             "It cannot be empty!!!");

我有提交按钮的课程

public class QuestionWindow extends Window {
    @Autowired
    private transient VaadinStringEditor editor;

    private Button createSaveButton() {
        Button saveButton = new Button(i18n.getWithDefault("newAseQuestions.save", "Übernehmen"));

        saveButton.addClickListener(e -> {
            if (editor.getBinder.validate()) {
                Notification.show("This is the caption OK", "This is the description",
                        Notification.Type.HUMANIZED_MESSAGE);
            } else {
                Notification.show("This is the caption", "This is the description",
                        Notification.Type.HUMANIZED_MESSAGE);
                System.out.println("kurwa");
            }

            saveAse();
        });
        return saveButton;
    }

1 个答案:

答案 0 :(得分:2)

好的,假设我们没有这个POJO:

public class Person {
    private String firstname;
    private String lastname;

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

}

我们想要编辑它。 所以我们构建以下形式:

public class Form {
    private TextField firstname;
    private TextField lastname;

    private Binder<Person> binder = new Binder<>();

    void bindFields() {
        binder.forField(firstname).withValidator(textRequiredValidator())
            .asRequired("Mandatory").bind(Person::getFirstname, Person::setFirstname);
        binder.forField(lastname).withValidator(textRequiredValidator())
            .asRequired("Mandatory").bind(Person::getLastname, Person::setLastname);
    }

    public void setDatasource(Person person) {
        binder.setBean(person);
    }

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

    public boolean validate() {
        BinderValidationStatus<Person> status = binder.validate();
        return status.isOk();
    }
}

为了使用这个表单,我们需要先调用 bindFields (例如构造函数,init)。

比控制器左右与我们想要编辑的人一起调用 setDatasource

在此之后,用户可以填写或编辑表单,当用户完成表单状态时,可以通过验证检索。

如果您需要字段中的错误,请从 BinderValidationStatus 中获取错误。

有关详细信息,请查看https://vaadin.com/docs/v8/framework/datamodel/datamodel-forms.html