###### tags: `Template Notes` `Java` # WOS - Part 3 Starter Template: --- - So now we have 2 pages running and 1 of them is dislaying some seed data - Now we need to add some validations - Update the way we create our forms - And post new data ## (className)Model: - Adding validations to existing table - Here are some common options - Don't forget to Command + Shift + o ``` @NotBlank @Size(min=1, max=255, message="Needs to be between 1-255 characters") @Min(value = 1, message = "Age must be at least 1") @Min(value = 59, message = "Age must not be more than 50") ``` ## form.jsp: - The typical form way we are not going to use so for now we will just comment it out but when starting it doesn't hurt to use this way to get the styling in place ``` <%-- <form action="/pet/create"> <section> <label for="name">Name</label> <input type="text" name="name" id="" /> </section> <section> <label for="species">Species</label> <input type="text" name="species" id="" /> </section> <section> <label for="gender">Gender</label> <label for="">Female</label> <input type="radio" name="gender" id="" /> <label for="">Male</label> <input type="radio" name="gender" id="" /> </section> <section> <label for="age">Age</label> <input type="number" name="age" id="" /> </section> <button>Submit</button> </form> --%> ``` - Instead we will use the following example: - form:form ``` <form:form action="/pet/add" method="POST" modelAttribute="newPet"> <section> <form:label path="name" class="form-label">Name</form:label> <form:input path="name" class="form-control" type="text"/> <form:errors path="name" class="text-danger" /> </section> <section> <form:label path="species" class="form-label">Species</form:label> <form:input path="species" class="form-control" type="text"/> <form:errors path="species" class="text-danger" /> </section> <section> <form:label class="form-label" path="gender">Gender</form:label> <section> <form:label path="gender" class="form-label">Female</form:label> <form:radiobutton class="form-check-input" lable="Female" path="gender" value="1"/> </section> <section> <form:label path="gender" class="form-label">Male</form:label> <form:radiobutton class="form-check-input" lable="Male" path="gender" value="0"/> </section> </section> <section> <form:label path="age" class="form-label">Age</form:label> <form:input path="age" class="form-control" type="number"/> <form:errors path="age" class="text-danger" /> </section> <button class="btn btn-primary">Submit</button> </form:form> ``` ## Other changes needed to make the above new form work ### controller file: ``` @GetMapping("/pet/create") public String createPet(@ModelAttribute("newPet") Pet newPet) { return "newPet.jsp"; } @PostMapping("/pet/add") public String addPet(@Valid @ModelAttribute("newPet") Pet newPet, BindingResult result) { petService.create(newPet); return "redirect:/"; } ``` ### service file: ``` public Pet create(Pet newPet) { return petRepository.save(newPet); } ``` ## Now: - At this point you should be able to post to the database. - However any validation errors are not going to show up on page the way we typically want it to it will show as an error ## Controller File: - So lets add some code to allow our errors to show - Edit the post function to at the if check ``` @PostMapping("/pet/add") public String addPet(@Valid @ModelAttribute("newPet") Pet newPet, BindingResult result) { if(result.hasErrors()) { return "newPet.jsp"; } petService.create(newPet); return "redirect:/"; } ```