Why this Thymeleaf form binding with selection field does not work?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Why this Thymeleaf form binding with selection field does not work?



I have two models, Patient and Study. In the Study model, I want to use Patient's Id as a foreign key. My Study Model (without getter/setter) is as below


Patient


Study


Study


Patient


Study


@Entity
@Table(name = "Study")
public class Study

@Id
@Column(name = "study_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@NotNull
@ManyToOne
@JoinColumn(name = "patient_id")
private Patient patient;

@NotNull
@Column(name = "description")
private String description;

@NotNull
@Column(name = "status")
private String status;



EDIT : Adding Patient class (without getter/setter) as well


@Entity
@Table(name="Patient")
public class Patient

@Id
@Column(name="patient_id")
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@NotNull
@Column(name="name")
private String name;

@Column(name="sex")
private String sex;

@Column(name="date_of_birth")
private Date dateOfBirth;



I am using Thymeleaf and selection of patient in my scheduleStudy.html is shown below


scheduleStudy.html


<form method="POST" th:action="@/scheduleStudy" th:object="$study">

<p>
Select Patient<select th:field="*patient"
required="required" class="form-control">

<option th:each="patient: $patientList" th:value="$patient.id"
th:text="$patient.name"></option>
</select>
</p>



The form is loading successfully with list of Patients in dropdown. However, when I am submitting the form after filling out all the fields, I am receiving:



Validation failed for object='study'. Error count: 1 error on browser.



Also the controller entries for Study form


Study


@GetMapping("/scheduleStudy")
public String addSchedule(Model model)
List<Patient> patientList = patientService.getPatients();
model.addAttribute("patientList", patientList);
model.addAttribute("study", new Study());
return "scheduleStudy";


@PostMapping("/scheduleStudy")
public void processAddSchedule(@ModelAttribute Study study)
studyService.addStudy(study);




I have just started using Thymeleaf and I think I have dome some mistake in the patient field. Could anyone please help?


patient



EDIT 2: I have updated the method for POST request, but patient is still null in controller. The previous browser error is gone of course.


patient


@RequestMapping(value = "/scheduleStudy", method = RequestMethod.POST)
public String processAddSchedule(Model model, @Valid @ModelAttribute("study") Study study,
BindingResult bindingResult)
if (bindingResult.hasErrors())

else
studyService.addStudy(study);






Try change public void processAddSchedule(@ModelAttribute Study study) to public void processAddSchedule(@ModelAttribute @Valid Study study)
– Do Nhu Vy
Jul 28 at 14:12


public void processAddSchedule(@ModelAttribute Study study)


public void processAddSchedule(@ModelAttribute @Valid Study study)





@DoNhuVy: Same result. How was that change supposed to help anyway?
– user3274247
Jul 28 at 14:24





I think @Valid must be used and put it at somewhere.
– Do Nhu Vy
Jul 28 at 14:25



@Valid





give your patient class
– sajib
Jul 28 at 14:30





@sajib: Hi sajib, kindly find the edit.
– user3274247
Jul 28 at 14:37




1 Answer
1



I was working on a similar task. As far as I learned when you post the study form Thymeleaf sends all fields as parameters to a POST request. It converts the Patient object to a String with toString(). Then when assembling the Study object it must convert the Patient back to the Object form. I solved this by registering 2 converters (toString and fromString) with the standard conversionService for Patient.





many thanks for your input. Could you please share your code regarding the same ?
– user3274247
Aug 6 at 16:51





For certain reasons I cannot share my code but you'll solve your problems with converters. Look at the parameters of the POST request, register the converters and implement toString for all entity classes.
– Xaltotun
Aug 7 at 9:57






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS