Shrikant Bochare
Shrikant Bochare

Reputation: 1

Bean Validaton : org.springframework.web.bind.MethodArgumentNotValidException

I have added validation constraints on fields of class which maps to the input fields of registration form, after invalid input I want to show same form with error message populated with it. I am using spring bean validation and following all the steps but the handler method which I mapped to process the form input is not executing and instead of populating error fields with BindingResult it is Giving exception .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException

Class :

public class FacultyPojo {

    private int id;

    @Pattern(regexp = "^[a-zA-Z ]{5,}$", message = "Name can contain small letters, capital letters and spaces" +
            "with minimum size of 5 characters")
    private String name;

    @Pattern(regexp = "^(FIT|FCS|FCE|FME|FEE|FET)\\\\d{6}$\\n" , message = "Faculty Id must start with FIT or FCS or FCE or FME or FEE or FET" +
            "followed by 6 digit number")
    private String facultyId;

    @Email(message = "Enter valid email address")
    private String email;

    @Pattern(regexp = "^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[@_])[A-Za-z0-9@_]{5,}$" ,message = "Password must contain at least one small letter" +
            ", one capital letter, one number and special character like @ or _  with minimum length of 5 characters")
    private String password;



costructors...
getters...
setters..

}

the handler methods are

@PostMapping("/faculty/registration/otpVerification")
    public String otpVerification2(Model model, Principal p)
    {
        if(p == null) {
            model.addAttribute("PageName", "Faculty_registration2");
            FacultyPojo facultyPojo = new FacultyPojo();
            model.addAttribute("facultyPojo", facultyPojo);

            return "Template";
        }
        else
        {
            System.out.println("no");

            return "redirect:/home/loginSuccess";
        }
    }





    @PostMapping("/faculty/registrationDetails")
    public String FacultyRegDetails(@ModelAttribute("facultyPojo") @Valid FacultyPojo facultyPojo, Principal p, BindingResult bindingResult, Model model)
    {
        if(p == null) {
            if(bindingResult.hasErrors())
            {
                model.addAttribute("PageName", "Faculty_registration2");
                return "Template";
            }
            {
                Faculty faculty = new Faculty(facultyPojo.getName(), facultyPojo.getFacultyId(), facultyPojo.getEmail(), passwordEncoder.encode(facultyPojo.getPassword()));
                faculty.addRole("ROLE_FACULTY");
                ProfilePic profilePic = new ProfilePic("default_pic.jpg");
                profilePic.setFaculty(faculty);
                faculty.setProfilePic(profilePic);
                serviceFacultyDao.saveFaculty(faculty);
                return "redirect:/home/login";
            }
        }
        else
        {
            return "redirect:/home/loginSuccess";
        }
    }

Handler method FacultyRegdetails() is not executing.......................

Form template is

<div class="row m-2" th:fragment="Faculty_registration2">
    <div class="col-md-6 offset-md-3 rounded loginContainer p-5">
        <h3 class="text-center text-Success">REGISTRATION</h3>
        <form th:action="@{/home/faculty/registrationDetails}" method="post" th:object="${facultyPojo}">
            <div class="form-floating mb-2">
                <input type="text" name="facultyId" id="facultyId" class="form-control" placeholder="BT24F06F006" th:field="*{facultyId}">
                <label for="facId">Enter faculty Id</label>
                <p th:if="${#fields.hasErrors('facultyId')}" th:errors="*{facultyId}"></p>
            </div>
            <div class="form-floating mb-2">
                <input type="text" name="name" id="name" class="form-control" placeholder="FullName" th:field="*{name}">
                <label for="name">Enter Your Full Name</label>
                <p th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></p>
            </div>
            <div class="form-floating mb-2">
                <input type="email" name="email" id="email" class="form-control" placeholder="[email protected]" th:field="*{email}">
                <label for="email">Enter Your  Email</label>
                <p th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></p>
            </div>
            <div class="form-floating">
                <input type="password" name="password" id="password" class="form-control" placeholder="Password" th:field="*{password}">
                <label for="password">Enter Password</label>
                <p th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></p>
            </div>
            <div class="row mt-3">
                <div class="col-6 text-center">
                    <button type="submit" class="btn btn-primary ">Submit</button>
                </div>
                <div class="col-6 text-center">
                    <a th:href="@{/home/login}" class="btn btn-secondary text-decoration-none">Login</a>
                </div>
            </div>
        </form>
    </div>
</div>


Upvotes: 0

Views: 21

Answers (0)

Related Questions