1. What is the purpose of the Spring Validator interface?
Why: The Validator interface lets you perform validation programmatically. You write the checks yourself in Java code.
2. Which two methods must a class implement when it implements the Spring Validator interface?
Why: A Validator must implement supports() and validate().
3. What is the job of the supports() method of a Validator?
Why: supports() receives a class and returns true if this validator can validate objects of that class.
4. What is the Errors object used for inside the validate() method?
Why: The Errors object collects the validation errors. You add an error to it whenever a check fails.
5. What does ValidationUtils.rejectIfEmpty() do?
Why: ValidationUtils.rejectIfEmpty() is a helper method. It adds an error for a field when the field value is null or empty.
6. Which two values do you pass to the Errors.rejectValue() method?
Why: rejectValue() takes the name of the field that failed validation and an error code that describes the problem.
7. How is an error code added by a Validator turned into a readable message for the user?
Why: Error codes are resolved using a MessageSource. It usually reads the messages from a properties file.
8. In a Spring MVC controller, how do you apply a custom Validator manually?
Why: In a controller you call validate() yourself. You pass the object to check and the BindingResult that will hold the errors.
9. How can a controller check if validation found any errors?
Why: BindingResult.hasErrors() returns true if any validation error was registered.
10. What does a controller usually do when validation errors are found after a form submission?
Why: When there are errors, the controller shows the same form view again. The error messages are displayed next to the fields.
11. The Spring Validator interface is an alternative to which kind of validation?
Why: Spring Validator uses Java code for validation. Jakarta Bean Validation uses annotations on the fields instead.
12. Which Spring form tag displays the validation error messages of a field in a JSP page?
Why: The <form:errors> tag shows the error messages registered for a given field.
13. The first parameter of the validate() method is of type Object. What should you do with it?
Why: The target is passed as a general Object. You cast it to the real type so you can read its properties and validate them.
14. In which package is the Spring Validator interface found?
Why: The Spring Validator interface, along with Errors and ValidationUtils, is in the org.springframework.validation package.