Form validations in spring mvc.

Form Validations in Spring MVC:

 

org.springframework.validation.Errors;

org.springframework.validation.ValidationUtils;

 

ValidationUtils provide us the validate(Object target, Errors errors) which needs to be overridden.

target is the modal that needs to be validated and the  errors has the validation errors.

 

Simple validation class:

 

@Component

public class sampleValidator {

       public boolean supports(Class<?> clazz) {

              return Job.class.isAssignableFrom(clazz);

       }

 

       public void validate(Object target, Errors errors) {

             

              User user = (User)target;

               

              if(job.getWeekDays().length==0){

                     errors.rejectValue("skills", "required.skills","Please select skills.");

              }

             

              ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",

                           "required.name", "Please enter name.");

 

             

       }

}

 

Modal:

 

public class User implements Serializable {

      

       String name;

       String [] skills;

 

      

       public Job(){

             

       }

/* setter getters*/

}

 

User form:

Spring Jstl tags allow us to bind the form variables to modal values very simply…

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

 

 

<form:form method="post" modelAttribute="user">

              <form:errors path="*" cssClass="error" element="div" />

              <fieldset>

 

              <legend>Registration</legend>

 

              <div class="control-group">

              <label class="control-label">Name</label>

              <div class="controls">

              <form:input path="name" class="input-large" type="text" rel="popover" data-content="Enter name." data-original-title=”Name"/>

              <form:errors path="name" cssClass="error" />

              </div>

              </div>

 

              <div class="control-group">

              <label class="control-label">Skills :</label>

              <div class=" form-inline">                            

              <form:checkboxes items="${user.skills}" path="skills" cssClass="checkbox"/>

              <form:errors path="skils" cssClass="error" />

              </div>

              </div>

             

              <div class="control-group">

              <label class="control-label"></label>

              <div class="controls">

              <button type="submit" class="btn btn-success" > Register </button>

              </div>

              </div>

 

              </fieldset>

 

             

</form:form>

Controller:

 

The @ModelAttribute binds the form values to the method variable “user”.

BindingResult The binds the results if any in the model attribute.

 

@Controller

@RequestMapping("/")

public class UserController {

@RequestMapping(value = "register", method = RequestMethod.POST)

       public String submitUser(@ModelAttribute("user") User user,

                     BindingResult result,ModelMap model) {

              validator.validate(user, result);

              if (result.hasErrors()) {

                     if(user.getSkills().length==0){

                           user.setSkills (new String[]{"java","spring","struts"});

                     }

                     model.addAttribute("user", user);

                     return "register";

                    

              }

              /* logic for registering user */

              return "register";

       }

 

}

 tags:validations, spring3, spring 3form validations, checkbox validation in spring

Comments

Popular posts from this blog

Salesforce concepts in Telugu: Salesforce యొక్క ముఖ్య అవగణాలను తెలుగులో

Learn more about our updated Terms of Service