In my spring MVC validation, the order of my error messages changes randomly, I would like the messages to be in the same order as they appear on the page.

My AccountForm.java class looks like:

@NotNull(message = "Account name cannot be empty.")
@Size(min=3, max=50, message="Account name must be between 3 and 50 characters long.")
private String accountName;

@NotNull(message = "Company name cannot be empty.")
@Size(min=3, max=50, message="Company name must be between 3 and 50 characters long.")
private String companyName;

And I also append some custom errors in my controller:

   public ModelAndView create(@Valid AccountForm accountForm, BindingResult bindingResult) {

    ModelAndView mav = new ModelAndView("accounts/new");
    mav.addObject("errors", bindingResult.getAllErrors());
    mav.addObject("accountForm", accountForm);

    if (!bindingResult.hasErrors()) {
        if(accountService.findByAccountName(accountForm.getAccountName()) != null) {
            bindingResult.addError(new ObjectError("accountName", "Account name is already is use"));
        }
        ..
        ..
    }

    if(bindingResult.hasErrors() {
      return mav;
    }

    ..

When I hit submit on the form, the order of the messages keeps changing.

I render the errors in my view using:

        <#list errors as error>
            <li>${error.defaultMessage}</li>
        </#list>

Can this be fixed?

Edit
Report