Validating forms input parameters

In a web application, validation of the web form data is very important, because end users can submit any thing. Suppose in an application, a user submits the account form by filling in the account name, then it could create the new account in the bank with account holder name. So, we have to ensure the validity of the form data before creating the new record in the database. You do not need to handle the validation logic in the handler method. Spring provides support for the JSR-303 API. As of Spring 3.0, Spring MVC supports this Java Validation API. There isn't much configuration required to configure the Java Validation API in your Spring web application-you just add the implementation of this API in your application class path such as Hibernate Validator.

The Java Validation API has several annotations to validate the properties of the Command object. We can place constraints on the value of the properties of the Command object. In this chapter, I have not explored all these annotations, but let's see the following examples with some of these annotations:

    package com.packt.patterninspring.chapter10.bankapp.model; 
 
    import javax.validation.constraints.NotNull; 
    import javax.validation.constraints.Size; 
 
    public class Account{ 
    
     // Not null 
     @NotNull 
     Long id; 
     // Not null 
     @NotNull 
     Long balance; 
     // Not null, from 5 to 30 characters 
     @NotNull 
     @Size(min=2, max=30) 
     String name; 
    
     public Long getId() { 
         return id; 
     } 
     public void setId(Long id) { 
         this.id = id; 
     } 
     public Long getBalance() { 
         return balance; 
     } 
     public void setBalance(Long balance) { 
         this.balance = balance; 
     } 
     public String getName() { 
         return name; 
     } 
     public void setName(String name) { 
         this.name = name; 
     } 
     @Override 
     public String toString() { 
         return "Account [id=" + id + ", balance=" + balance + ", name=" + name + "]"; 
     } 
    
    } 

As you can see in the preceding code, the properties of the Account class are now annotated with @NotNull to ensure that the value must not be null, and some properties are also annotated with the @Size annotation to ensure the count of characters between the minimum and maximum lengths.

Only annotating the properties of the Account object is not enough. We have to annotate the save() method argument of the AccountController class as follows:

    package com.packt.patterninspring.chapter10.bankapp.web.controller; 
    .... 
    .... 
    @Controller 
    public class AccountController { 
    
     .... 
     @PostMapping(value = "/open-account") 
     public String save (@Valid @ModelAttribute("account") Account account, Errors errors){ 
         if (errors.hasErrors()) { 
               return "accountForm"; 
         } 
         accountService.open(account); 
         return "redirect:/accounts/"+account.getId(); 
     } 
     .... 
    } 

As you can see in the preceding code, the Account parameter is now annotated with @Valid to indicate to Spring that the command object has validation constraints that should be enforced. Let's see the output when we submit the web open account form while filling invalid data:

As I had submitted this form without data, it has been redirected to the same page with validation errors. Spring also allows you to customize these validation messages by configuring these messages into the properties file.

As of now, in this chapter, you have learned about the controller component of the MVC pattern. You also learned how to create and configure in a web application. Let's explore another component of the MVC pattern, view, in the upcoming section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset