Sunday 11 April 2021

Bean validation: Customize error message using message attribute

Every constraint has a ‘message’ attribute, using this you can customize the validation error messages.

 

Example

@Size(min=5, message="Name must have atlease 5 characters")

private String name;

 

Find the below working application.

 

Employee.java

package com.sample.app.model;

import javax.validation.constraints.Size;

public class Employee {

	private int id;

	@Size(min=5, message="Name must have atlease 5 characters")
	private String name;

	public Employee(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}

 

App.java

package com.sample.app;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import com.sample.app.model.Employee;

public class App {
	private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
	private static Validator validator = validatorFactory.getValidator();

	private static void validateBean(Employee emp) {
		System.out.println("************************************");
		Set<ConstraintViolation<Employee>> validationErrors = validator.validate(emp);

		if (validationErrors.size() == 0) {
			System.out.println("No validation errors....");
		}

		for (ConstraintViolation<Employee> violation : validationErrors) {
			System.out.println(violation.getPropertyPath() + "," + violation.getMessage());
		}
		System.out.println("");
	}

	public static void main(String args[]) {
		Employee emp1 = new Employee(1, "Ram");
		System.out.println("Validation Errors for emp1");
		validateBean(emp1);

		Employee emp2 = new Employee(2, "RamaKrishna");
		System.out.println("Validation Errors for emp2");
		validateBean(emp2);

	}
}

 

Run App.java, you will see below messages in console.

Validation Errors for emp1
************************************
name,Name must have atlease 5 characters

Validation Errors for emp2
************************************
No validation errors....

 

You can use expression language to access the validated value.

 

Example

@Size(min=5, message="Name '${validatedValue}' is not upto the requirement. Name must have atleast 5 characters")

private String name;

 

${validatedValue}: It gets the validated value. Let’s update Employee class with the new message using expression language.

 

Employee.java

package com.sample.app.model;

import javax.validation.constraints.Size;

public class Employee {

	private int id;

	@Size(min=5, message="Name '${validatedValue}' is not upto the requirement. Name must have atleast 5 characters")
	private String name;

	public Employee(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}

 

Run App.java, you will see below messages in console.

Validation Errors for emp1
************************************
name,Name 'Ram' is not upto the requirement. Name must have atleast 5 characters

Validation Errors for emp2
************************************
No validation errors....

 

you can even include the constraint boundaries using Expression language.

 

For example, {min} is used to access the value of min attribute.

 

@Size(min=5, message="Name '${validatedValue}' is not upto the requirement. Name must have atleast {min} characters")

private String name;

 

Employee.java

package com.sample.app.model;

import javax.validation.constraints.Size;

public class Employee {

	private int id;

	@Size(min=5, message="Name '${validatedValue}' is not upto the requirement. Name must have atleast {min} characters")
	private String name;

	public Employee(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}

 

Run App.java, you will see below messages in console.

Validation Errors for emp1
************************************
name,Name 'Ram' is not upto the requirement. Name must have atleast 5 characters

Validation Errors for emp2
************************************
No validation errors....

 

 

 

 

 

 

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment