@Valid  로 검증을 할 때 발생하는 에러

 

생각보다 간단한 에러입니다.

 

[ 문제 상황 ]

1. 회원가입 페이지의 Domain에 이름과 연락처 등에 대해 @NotEmpty 을 붙쳐줬습니다.

2. 컨트럴러에서 @Valid, BindingResult 피라미터를 넣어줍니다.

3. 검증에러가 발생시 bindingResult 객체에 에러가 담기고, 에러가 발생하면 다시 재입력받을 수 있는 페이지를 구축하는 것입니다.

 

* 발생한 문제: 검증이 발생하는 것은 파악이 되지만, bindingResult 객체가 제대로 작동하지 않음

 

 

 

 

[ 문제 해결 ]

검증은 피라미터 순서에 영향을 받습니다.

따라서

@PostMapping("/join")
    public String joinExecute(
            @Valid MemberForm memberForm,
            @RequestParam String password2,
            RedirectAttributes redirectAttributes,
            Model model,BindingResult bindingResult)

위의 코드처럼 @Valid 후 BindingResult 순서가 바로 붙어있는게 아닌 저렇게 다른 피라미터를 먼저 받을 경우 원하는 동작을 안합니다.

 

@PostMapping("/join")
    public String joinExecute(
            @Valid MemberForm memberForm,BindingResult bindingResult,
            @RequestParam String password2,
            RedirectAttributes redirectAttributes,
            Model model)

이렇게 피라미터 순서를 제대로 배치하면 해결됩니다.

+ Recent posts