Giter Club home page Giter Club logo

spring-custom-validation's Introduction

Spring-Custom-Validation

Here I come with a project on “Spring MVC Custom validation”. Let’s see what we got here:

Author :

Md Mamun Abdul Kayum

Technologies :

   Spring MVC, Bootstrap 3.x

Required dependency for custom validation purpose:

   <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>

Tested on Application Servers :

Tomcat 8, Glassfish 4

Build Tool:

Maven

Description:

For achieving spring mvc custom validation:

  1. you have to add "form:errors" tad at the form input field like:
               <div class="form-group">
                   <label class="control-label col-sm-2" for="name">Customer Name:</label>
                   <div class="col-sm-10">
                       <form:input path="name" value="" type="text" class="form-control" placeholder="Customer Name"/>
                   <font color="red"><form:errors path="name"></form:errors></font>
                   </div>
               </div>

Add this error tag to those fields you want to validate.

  1. You must a propertise file(Say message.properties) which will contains your validation messages. In our case it is "messages.properties" file located at webapp/WEB-INF/i18n/messages.properties It contains our messages:
# validation message for product 
NotEmpty.productName=enter a name
NotEmpty.quality=enter a quality
NotEmpty.measureUnit=enter a measure unit
NotEmpty.quantity=enter a quantity
#validation message for customer
NotEmpty.name=enter a name
NotEmpty.education=enter your education
NotEmpty.address=enter your address
NotEmpty.designation=enter a designation

Range.quantity=quantity range should be between 1 - 100
Range.age=age should be greater than 5
  1. Now you have to add a validator class for our form validation, it contains validation logics:
if (target instanceof CustomerForm) {
            CustomerForm customerForm = (CustomerForm) target;
            if (!StringUtils.hasText(customerForm.getName())) {
                errors.rejectValue("name", "NotEmpty.name");
            }
            if (!StringUtils.hasText(customerForm.getEducation())) {
                errors.rejectValue("education", "NotEmpty.education");
            }
            if (!StringUtils.hasText(customerForm.getAddress())) {
                errors.rejectValue("address", "NotEmpty.address");
            }
            if (!StringUtils.hasText(customerForm.getDesignation())) {
                errors.rejectValue("designation", "NotEmpty.designation");
            }
            if (customerForm.getAge() < 5) {
                errors.rejectValue("age", "Range.age");
            }
        }

From here we are sending errors if a filed failed to a validation logic. 4. Now from Controller class you have to check if a filed have any validation error:
CustomValidator userValidator = new CustomValidator(); userValidator.validate(customerObject, result); if (result.hasErrors()) { return "addCustomer"; }

What we will get here:

addcustomer

addproduct

spring-custom-validation's People

Contributors

md-mamunabdulkayum avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.