Spring REST API Validation

In Spring MVC, just annotate a @Valid on the @RequestBody to fire the validation process. Note For complete source code, please refer to this – Spring Boot Ajax example P.S Tested with Spring Boot 1.5.1.RELEASE (Spring 4.3.6.RELEASE) 1. JSR 303 Validation Add JSR303 annotations on a bean. package com.mkyong.model; import org.hibernate.validator.constraints.NotBlank; public class SearchCriteria { …

Read more

Spring 3 MVC and JSR303 @Valid example

In Spring 3, you can enable “mvc:annotation-driven” to support JSR303 bean validation via @Valid annotation, if any JSR 303 validator framework on the classpath. Note Hibernate Validator is the reference implementation for JSR 303 In this tutorial, we show you how to integrate Hibernate validator with Spring MVC, via @Valid annotation, to perform bean validation …

Read more

Multiple Components Validator in JSF 2.0

In JSF, there is no official way to validate multiple components or fields. To solve it, you need to create a custom validator. In this tutorial, we will show you two unofficial ways to create a validator to validate multiple components – password and confirm password. Two ways : 1. Register PostValidateEvent, puts validation inside. …

Read more

Custom validator in JSF 2.0

In this article, we will show you how to create a custom validator in JSF 2.0 Steps Create a validator class by implements javax.faces.validator.Validator interface. Override validate() method. Assign an unique validator ID via @FacesValidator annotation. Reference custom validator class to JSF component via f:validator tag. A detail guide to create a custom validator name …

Read more

Customize validation error message in JSF 2.0

The standard JSF conversion and validation error messages are too detail, technical or sometime, not really human readable. In this article, it shows you how to customize standard conversion or validation error message in JSF 2.0. Summary Guide Find your message key from jsf-api-2.x.jar, “Messages.properties” file. Create your own properties file, and put the same …

Read more

How to skip validation in JSF

Problem See following JSF example : <h:inputSecret id="password" value="#{user.password}" size="20" required="true" label="Password"> <f:validateRegex pattern="((?=.*\d).{6,20})" /> </h:inputSecret> <h:message for="password" style="color:red" /> <h:commandButton value="Cancel" action="cancel" /> <h:commandButton value="Submit" action="result" /> If you click on the “cancel” button, the password validation will be fired and stop you proceed on the cancel page, which is doesn’t make sense. Is …

Read more

JSF 2 validateRegex example

“f:validateRegex” is a new validator tag in JSF 2.0, which is used to validate JSF component with a given regular expression pattern. For example, <h:inputSecret id="password" value="#{user.password}"> <f:validateRegex pattern="((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})" /> </h:inputSecret> The above regex pattern is required 6 to 20 characters string with at least one digit, one upper case letter, one lower case letter …

Read more

JSF 2 validateRequired example

“f:validateRequired” is a new validator tag in JSF 2.0, which is used to make sure the input field is not empty. For example, <h:inputSecret id="password" value="#{user.password}"> <f:validateRequired /> </h:inputSecret> Alternatively, you can use the “required” attribute also, both are doing the same empty value validation. <h:inputSecret id="password" value="#{user.password}" required="true" /> “f:validateRequired” example A JSF 2.0 …

Read more

JSF 2 validateDoubleRange example

“f:validateDoubleRange” is a JSF range validator tag, which is used to validate the range of a floating point value. For example, <h:inputText id="salary" value="#{user.salary}"> <f:validateDoubleRange minimum="10.11" maximum="10000.11" /> </h:inputText> When this form is submitted, the validator will make sure the “salary” value is within the range from “10.11” to “10000.11”. “f:validateDoubleRange” example A JSF 2.0 …

Read more

JSF 2 validateLongRange example

“f:validateLongRange” is a JSF range validator tag, which is used to check the range of a numeric value. For example, <h:inputText id="age" value="#{user.age}"> <f:validateLongRange minimum="1" maximum="150" /> </h:inputText> When this form is submitted, the validator will make sure the “age” value is within the range from 1 to 150. “f:validateLongRange” example A JSF 2.0 example …

Read more