Display error messages directly in Vaadin 7

I am developing a web application in Vaadin that includes a large number of forms. Currently, all screens have been created, and I led them past several test users (4) to check their usability. All of them had the same comment; when a validation error has occurred, it is not clear what the problem is. None of them thought about hovering over the error indicator (if they even noticed the indicator) to get the exact error message.

I read in Vaadin's book that the placement of the error indicator is controlled by the layout that contains the component. However, it does not seem to say anything about directly displaying the error message. Is it possible to do this (preferably, without having to run your own set of widgets)?

Thanks,

William

+4
source share
3 answers

I do not think that what you have in mind is implemented using the core vaadin components.

: , . , , . , , , .

+3

:

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.vaadin.server.ErrorMessage;
import com.vaadin.server.Page;
import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;

public class ErrorUtils {
    public static List<String> getComponentError(
            final AbstractComponent[] componentArray) {
        List<String> errorList = new ArrayList<String>();

        for (AbstractComponent component : componentArray) {
            ErrorMessage errorMessage = component.getErrorMessage();
            if (errorMessage != null) {
                errorList.add(errorMessage.getFormattedHtmlMessage());
            }
        }

        return errorList;
    }

    public static List<String> getComponentError(
            final Collection<?> componentCollection) {
        AbstractComponent[] componentArray = componentCollection
                .toArray(new AbstractComponent[] {});
        return ErrorUtils.getComponentError(componentArray);
    }

    public static void showComponentErrors(
            final AbstractComponent[] componentArray) {
        List<String> errorList = ErrorUtils.getComponentError(componentArray);

        String error = StringUtils.join(errorList, "\n");

        Notification notification = new Notification("Error", error,
                Type.ERROR_MESSAGE, true);

        notification.show(Page.getCurrent());
    }

    public static void showComponentErrors(
            final Collection<?> componentCollection) {
        AbstractComponent[] componentArray = componentCollection
                .toArray(new AbstractComponent[] {});

        ErrorUtils.showComponentErrors(componentArray);
    }
}

, , :

private void saveButtonClicked() {
    // this method is the handler of the click event of the [save] button

    try {
        this.fieldGroup.commit();
    } catch (CommitException e) {
        // Show all the validate errors:
        ErrorUtils.showComponentErrors(this.fieldGroup.getFields());

        return;
    }

    // save data, if there is no validate error
}
+2

Source: https://habr.com/ru/post/1528789/


All Articles