0

I'm skilling in form validation with spring boot and thymeleaf and i have problem: i can't do validation in form with two @ModelAttribute fields. The example like form validation om spring official site works correctly, but when I added two @model Attribute in post i get only error at webpage and no hints at form like in spring example.

Controller class:

@Controller
public class MyController {

    @Autowired
    InstructorRepository instructorRepository;

    @Autowired
    DetailRepository detailRepository;

    @GetMapping("/index")
    public String mainController(){
        return "index";
    }

    @GetMapping("/add")
    public String addInstructorForm(Model model){
        model.addAttribute("instructor", new Instructor());
        model.addAttribute("detail", new InstructorDetail());
        return "addInstructor";
    }

    @PostMapping("/add")
    public String submitForm(@Valid @ModelAttribute Instructor instructor, @ModelAttribute InstructorDetail instructorDetail, BindingResult bindingResult1){
       /* if (bindingResult.hasErrors()) {
            return "instructorsList";
        }
        instructor.setInstructorDetail(instructorDetail);
        instructorRepository.save(instructor);*/

        if (bindingResult1.hasErrors()) {
            return "addInstructor";
        }
        return "redirect:/instructorsList";
    }

    @GetMapping("/instructorsList")
    public String getList(Model model){
        Map map = new HashMap<>();
        List list = new ArrayList<Instructor>();
        list = instructorRepository.findAll();
        List resultList = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            Instructor instructor = (Instructor)list.get(i);
            InstructorDetail detail = detailRepository.getInstructorDetailById(instructor.getId());
            InstructorAndDetail iid = new InstructorAndDetail(instructor, detail);
            resultList.add(iid);
        }
        model.addAttribute("instructors", resultList);
        return "instructorsList";
    }

}

html form snippet:

<form action="#" data-th-action="@{/add}" data-th-object="${instructor}" method="post">
   <div class="form-group">
        <label for="1">First name</label>
        <input class="form-control" id="1" type="text" data-th-field="${instructor.firstName}" placeholder="John"/>
        <div data-th-if="${#fields.hasErrors('firstName')}" data-th-errors="${instructor.firstName}">name error</div>
   </div>
Manish Patel
  • 3,648
  • 1
  • 14
  • 22
NyansusCoder
  • 89
  • 3
  • 14

1 Answers1

0

There was next problem: then I add entity to thymeleaf form I passed only 2 fields (or one field after), but there was 3 fields with

@NotNull
@Size(min=2, max=30)

So when I commented them in my code the single field validation begin to work :). If you stucked at the same problem check that all you fields in class that marked @Valid annotation are mirrored in your form. (or have default valid values? UPD: dont work with valid defaults if they have no form mirroring)

NyansusCoder
  • 89
  • 3
  • 14