Populating a model parameter based on a dropdown selection

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Populating a model parameter based on a dropdown selection

niladridas07@gmail.com
Hello,

Let me first describe my use case briefly.

I  have a patient model. I am adding many patient to database. Then I am trying to add a study for patient.

In my study form I am using a dropdown to select a already added patient.

In my study model I have a field for patient.

The problem is, in the form I can see the dropdown for patients, I can select one of them, but upon submitting the form, I am not getting an error: Validation failed for object='study'. Error count: 1 error on browser.

I have two more field in the study form, which are text field. I am sure this two filed does not have any issue.

For my patient form, I have used text field only, and those worked absolutely fine. Here I am unable to add the selected value from the dropdown to the patient field of Study model.

Here is my html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
   

Schedule Study

    <form action="#" th:action="@{/scheduleStudy}" th:object="${study}" method="post">

         <select class="form-control" id="patientId" th:field="*{patient}">
        <option value="">Select Patient</option>
        <option th:each="patient : ${patientList}"
                th:value="${patient.id}"
                th:text="${patient.name}"></option>
        </select>

        <p>Description: <input type="text" th:field="*{description}" /></p>
        <p>Status: <input type="text" th:field="*{status}" /></p>

        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
</body>
</html> 

If you want to have a look into both of the model class, kindly refer to this post - https://stackoverflow.com/questions/51571933/loading-and-saving-a-form-field-populated-from-a-second-form-field-using-thymele?noredirect=1#comment90112046_51572750.

I am really stuck with this one. Kindly help. Either reply here or in the stackoverflow link, whichever you prefer.

Many thanks in advance.