使用fieldgroup属性vaadin进行验证

时间:2014-05-02 08:02:26

标签: java web frameworks vaadin

我有这个文本域

   firstNameField = new TextField("First Name");
    firstNameField.setRequired(true);
    firstNameField.setImmediate(true);
    firstNameField.setValidationVisible(true);
    firstNameField.setWidth(COMMON_FIELD_WIDTH);
    firstNameField.addValidator(new StringLengthValidator(
            "Allowed Length of Characters(2-30) ", 2, 30, true));

    firstNameField.setNullRepresentation(null);

此字段绑定到bean属性firstName

  binderFieldGroupPersonalDetails.bind(firstNameField, "firstName");

现在我正在使用

  binderFieldGroupPersonalDetails.commit;

提交时图标没有验证错误 但是悬停在fiels上时会显示requirederror

如何提示用户关于空字段?
我试过这个

   Button.ClickListener btnSaveContinuePersonalDetailsClickListener = new Button.ClickListener() {

    public void buttonClick(ClickEvent event) {
        try {
            // changes
                // the
                // tab
                // layout
                // to
                // next
                // tab
                // on
                // Successful
                // commit
            binderFieldGroupPersonalDetails.commit();
            TabSheetLayout
                    .setSelectedTab(contentAccountDetailsRegistrationForm);

        } catch (CommitException e) {


            //lets add up all the error msg for missing fields
            Collection<Field<?>> fields = binderFieldGroupPersonalDetails.getFields();
            Iterator<Field<?>> it = fields.iterator();
            String errorMessage = "";
            while(it.hasNext()){
                if(it.next().getValue()==null||it.next().getValue()==""){
                    errorMessage=errorMessage.concat("  "+it.next().getCaption()+" is required <br>");

                }
            }
            getUI().showNotification("<h6>"+errorMessage+"<h6>",Notification.TYPE_ERROR_MESSAGE);

                e.printStackTrace();
        }

    }
};  

1 个答案:

答案 0 :(得分:1)

binderFieldGroupPersonalDetails.setBuffered(true);

// A button to commit the buffer
button.addComponent(new Button("OK", new ClickListener() {
    @Override
    public void buttonClick(ClickEvent event) {
        try {
            binder.commit();
            Notification.show("Thanks!");
        } catch (CommitException e) {
            Notification.show(e.getMessage());
        }
    }
}));