2

Added the below jar's to my lib folder in WEB-INF:

  • classmate-1.0.0
  • javax.el-2.2.4
  • javax.el-api-2.2.4
  • jboss-logging-3.1.3.GA
  • validation-api-1.1.0.Final

The pieces of code I added are:

From model object:

public class UserDetails {

    @Pattern(regexp="(^0-9}*") 
    private String userName;   

    @Size(min=2,max=10)
    private String firstName;    
    private String lastName;
    private String emailId;
    private ArrayList<String> accountType;
    private ArrayList<String> gender;

    @Size(min=2,max=10)
    private Long accountNo;

FromController class:

@RequestMapping(value = "/UserAccount.html", method = RequestMethod.POST)
public ModelAndView userAccountForm(
        @Valid @ModelAttribute("user") UserDetails user,
        BindingResult result) {

    if (result.hasErrors()) {
        ModelAndView model1 = new ModelAndView("LoginForm");
        return model1;
    }

    ModelAndView model1 = new ModelAndView("UserAccount");
    return model1;
}

My dispatcher servlet has

<mvc:annotation-driven/>

Kindly let me know If I missed any basic thing.

madteapot
  • 2,208
  • 2
  • 19
  • 32
Hima Rayaprolu
  • 123
  • 1
  • 3
  • 14
  • @Pattern(regexp="(^0-9}*") :---- There is no open brace " { " – Pavan Mar 30 '15 at 11:21
  • @M.Deinum-The function of [@Size] and [@Pattern] didn't work.In my case i gave firstName value greater than 10 still it accepted without throwing the error.Same in the case of [@Pattern] – Hima Rayaprolu Mar 30 '15 at 12:21

3 Answers3

5

I got this working by adding the below jar file to my library.

hibernate-validator-5.1.3.Final

As I was learning through tutorials I missed adding this jar though it was mentioned.

Thanks everyone for the comments given.

Hima Rayaprolu
  • 123
  • 1
  • 3
  • 14
4
@Pattern(regexp = "[a-zA-Z0-9_.]*")
private String userName;


@Size(min=2, max=30)
private Long accountNo;

@Size(min=2,max=10)
private String firstName;

@Size annotation is correct.

Please check the way @Pattern is used. POJO class is correct. Just change the @Pattern annotation usage. Also I am sharing few sample examples regarding usage of @Pattern annotation.

http://www.journaldev.com/634/java-regular-expression-tutorial-with-examples

Bernhard
  • 4,855
  • 5
  • 39
  • 70
Pavan
  • 1,219
  • 13
  • 15
  • As suggested by you i have changed it to '@Pattern(regexp="[^0-9]*")' 'private String userName;'.But still it didn't work.Did i miss any basic settings?From the result i got i can say that the form validations are not happening at all with the use of [@Size] and [@Pattern]. – Hima Rayaprolu Mar 30 '15 at 12:30
0

You can add the following dependency in your pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation</artifactId>
</dependency>