5

I'm familiar with using Spring's <form:errors> tags to display validation errors for object properties, but how do you display an error at the class level?

Here's an example of what I'm talking about:

@ScriptAssert(lang = "javascript", 
        script = "_this.isExistingEmployee == true || (_this.phoneNumber != null && _this.address != null)", 
        message = "New hires must enter contact information")
public class JobApplicationForm {

    @NotBlank(message = "First Name is required")
    private String firstName;

    @NotBlank(message = "Last Name is required")
    private String lastName;

    @NotNull(message = "Please specify whether you are an existing employee in another area")
    private Boolean isExistingEmployee;

    private String phoneNumber;

    private String address;
}

@ScriptAssert here just confirms that if the applicant has indicated that they are an existing employee they can skip the contact info, but if not they must enter it. When this validation fails, the error is not on a given field but rather is on the class.

Within the form, I can show an error on a field with <form:errors path="firstName"> and I can display all errors (class and field) at once using <form:errors path="*"> but how can I isolate and display class level errors?

1 Answer 1

14

To show only class-level errors, leave the path empty.

<form:errors path="">

from the Spring docs:

  • path="*" - displays all errors
  • path="lastName" - displays all errors associated with the lastName field
  • if path is omitted - object errors only are displayed

ref: http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/view.html#view-jsp-formtaglib-errorstag

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.