Skip to content
This repository was archived by the owner on Apr 21, 2023. It is now read-only.
This repository was archived by the owner on Apr 21, 2023. It is now read-only.

Not all errors added when using same code on multiple fields #102

@wimdeblauwe

Description

@wimdeblauwe

Suppose this class:

public class UserCreationParameters {


    @Email(message = PROPERTY_IS_NOT_EMAIL)
    @NotEmpty(message = PROPERTY_SHOULD_NOT_BE_EMPTY)
    private String email;

    @NotEmpty(message = PROPERTY_SHOULD_NOT_BE_EMPTY)
    private String firstName;

    @NotEmpty(message = PROPERTY_SHOULD_NOT_BE_EMPTY)
    private String lastName;

    @NotEmpty(message = PROPERTY_SHOULD_NOT_BE_EMPTY)
    private String password;

   ... 
}

With the constants:

public final class ErrorCodes {
    public static final String PROPERTY_SHOULD_NOT_BE_EMPTY = "PROPERTY_SHOULD_NOT_BE_EMPTY";
    public static final String PROPERTY_IS_NOT_EMAIL = "PROPERTY_IS_NOT_EMAIL";
}

If I test this with the 4 fields of UserCreationParameters empty, I only get 1 item in the errors array:

I checked in SpringValidationWebErrorHandler and there are 4 errors in the BindingResult. However, since they map to the same key and they are put in a Set only 1 remains.

It is possible to support re-use of error codes to avoid having a lot of nearly the same translations of the error codes to error messages? I wrote a custom HttpErrorAttributesAdapter to add the name of the property in the resulting JSON so I can have the same error codes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions