简体   繁体   English

使用Binder进行表单验证

[英]Form validation with Binder

How to finish validation with sending all form data in Vaadin 8? 如何在Vaadin 8中发送所有表单数据来完成验证? Unfortunetly I dont understand binder concept :( I wrote a field validation but what now? It works. My user see when I demand that he fill out a field but is there any easy way to validate my all form? How can I "tell" to my save button that my form is valid? 不幸的是,我不理解活页夹的概念:(我写了一个字段验证但是现在该怎么办?它可以工作。我的用户看到我何时要求他填写一个字段,但是有什么简单的方法可以验证我的所有表格吗?如何“讲”到我的保存按钮,我的表格有效吗?

在此处输入图片说明

In my editor Im defining a validator 在我的编辑器中,我定义了验证器

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

    }

}

I have also an TextEditorConfiguration added: 我还添加了一个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;
    }
}

In my case there are plenty of editors like DateEditor and so on. 就我而言,有很多编辑器,例如DateEditor等。 UI Valtidation works well. UI验证效果很好。 Since one month I can not find a way how to connect it to submit button to prevent send a form. 由于一个月以来,我无法找到一种方法来连接它以提交按钮以防止发送表单。

In the form class I have defined all questions for example: 在表单类中,我定义了所有问题,例如:

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

And the class where I have a submit button 还有我有一个提交按钮的班级

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

OK lets assume we haven this POJO: 好的,假设我们没有这个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;
    }

}

And we want to edit it. 我们要编辑它。 So we build the following form: 因此,我们构建以下形式:

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

In order to use this form we need to call bindFields first (eg constructor, init). 为了使用这种形式,我们需要首先调用bindFields (例如,构造函数,init)。

Than a controller or so calls setDatasource with the person we want to edit. 比控制器,我们要编辑的人调用setDatasource

After this the user can fill or edit the form and when the user finishes the status of the form can be retrieved via validate . 之后,用户可以填写或编辑表单,完成后可以通过validate检索表单的状态。

If you need the errors from the fields you get them from the BinderValidationStatus . 如果需要字段中的错误,请从BinderValidationStatus中获取它们。

For more information look at https://vaadin.com/docs/v8/framework/datamodel/datamodel-forms.html 有关更多信息, 参见https://vaadin.com/docs/v8/framework/datamodel/datamodel-forms.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM