Example,
I have
@NotEmpty //tells you 'may not be empty' if the field is empty
@Length(min = 2, max = 35) //tells you 'length must be between 2 and 35' if the field is less than 2 or greater than 35
private String firstName;
Then I input an empty value.
It says, 'may not be empty length must be between 2 and 35'
Is it possible to tell spring to validate one at a time per field?
Use custom constraints for your field. For example, will use annotate
@StringField
.Then make some logic in
StringFieldValidator
class. This class implemented by an interfaceConstraintValidator <A extends Annotation, T>
.Then you can use annotation like:
And after all, you will have only one error message shown in right order.
A better solution would be to have some markup in your error message so that it formats nicely, such as a
<br />
, and use a CSS class to format the overall message. As noted in Bozho's comment, a user should be aware of everything that is incorrect.Yes it is possible. Just create your own annotation like this:
important part is the @ReportAsSingleViolation annotation