Search code examples
htmlmysqlspringspring-bootspring-data

Unable to save form tomysql database in spring boot thymeleaf application


I built a simply CRUB using spring boot and thymeleaf but the form does not save values to the database on submit. Here is my controller:

@Controller public class ClientController { private static final Logger LOG = LoggerFactory.getLogger(ClientController.class);

@Autowired
private ClientRepository clientRepository;


@GetMapping("/patients")
public String getAllClients(Model model) {
    List<Client> clients = clientRepository.findAll();
    model.addAttribute("clients", clients);
    return "patient/list-patients";
}

@GetMapping("/add-patient")
public String addClient(Model model) {
    model.addAttribute("client", new Client());
    return "patient/add-patient";
}


@PostMapping("/add-patient")
public String addClient(@Valid @ModelAttribute Client client, BindingResult bindingResult) {
    LOG.info("Saving client: {}", client);
    if (bindingResult.hasErrors()) {
    return "patient/add-patient";
    }
    clientRepository.save(client);
    return "redirect:/add-patient";
}

@GetMapping("/edit/{id}")
public String editClient(@PathVariable("id") Long id, Model model) {
    model.addAttribute("client", clientRepository.findById(id).orElse(null));
    return "patient-edit";
}



    @GetMapping("/delete/{id}")
    public String deleteClient(@PathVariable("id") Long id) {
        clientRepository.deleteById(id);
        return "redirect:/patients";
    }
}

The model has the following code:

@Entity
@Data
public class Client {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String patientId;
    
    @Size(min=2, max=30)
    private String firstName;

    @Size(min=2, max=30)
    private String lastName;
    private String otherName;
    private String gender;
    private String email;
    private Date dateOfBirth;
    private String ageGroup;
    private String phoneNumber;
    private String address;
    
//    Identity
    private String cardType;
    private String cardNumber;
    private String iDCardNumber;
    
    
    //getters and setters
}

And finally, the form is as follows:

< div >
    <
    form action = "#"
th: action = "@{/add-patient}"
method = "post" >
    <
    p >
    <
    label
for = "firstName" > First Name: < /label> <
    input type = "text"
id = "firstName"
name = "firstName" / >
    <
    /p> <
    p >
    <
    label
for = "lastName" > Last Name: < /label> <
    input type = "text"
id = "lastName"
name = "lastName" / >
    <
    /p> <
    p >
    <
    label
for = "otherName" > Other Name(s): < /label> <
    input type = "text"
id = "otherName"
name = "otherName" / >
    <
    /p> <
    p >
    <
    label
for = "dateOfBirth" > Date of Birth: < /label> <
    input type = "date"
id = "dateOfBirth"
name = "dateOfBirth" / >
    <
    /p> <
    p >
    <
    label
for = "address" > Address: < /label> <
    input type = "text"
id = "address"
name = "address" / >
    <
    /p> <
    p >
    <
    label
for = "email" > Email: < /label> <
    input type = "email"
id = "email"
name = "email" / >
    <
    /p> <
    p >
    <
    label
for = "phoneNumber" > Phone Number: < /label> <
    input type = "tel"
id = "phoneNumber"
name = "phoneNumber" / >
    <
    /p> <
    p >
    <
    label
for = "gender" > Gender: < /label> <
    select id = "gender"
name = "gender" >
    <
    option value = "Male" > Male < /option> <
    option value = "Female" > Female < /option> <
    option value = "Other" > Other < /option> <
    /select> <
    /p> <
    p >
    <
    label
for = "ageGroup" > Age Group: < /label> <
    select id = "ageGroup"
name = "ageGroup" >
    <
    option value = "Under 18" > Under 18 < /option> <
    option value = "18-25" > 18 - 25 < /option> <
    option value = "26-35" > 26 - 35 < /option> <
    option value = "36-45" > 36 - 45 < /option> <
    option value = "46-55" > 46 - 55 < /option> <
    option value = "Over 55" > Over 55 < /option> <
    /select> <
    /p> <
    p >
    <
    label
for = "cardType" > Card Type: < /label> <
    select id = "cardType"
name = "cardType" >
    <
    option value = "passport" > Passport < /option> <
    option value = "Ghana Card" > Ghana Card < /option> <
    /select> <
    /p> <
    p >
    <
    label
for = "cardNumber" > Card Number: < /label> <
    input type = "text"
id = "cardNumber"
name = "cardNumber" / >
    <
    /p> <
    input type = "submit"
value = "Save" / >
    <
    /form> <
    /div>

Solution

  • Solved it, apparently i have to update my controller with the method:

        @PostMapping("/add-patient")
    public String addClient(@ModelAttribute Client client) {
        LOG.info("Saving client: {}", client);
        
        // Format the date
        Date date = client.getDateOfBirth();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String strDate = dateFormat.format(date);
        
        client.setDateOfBirth(client.getDateOfBirth());
        
        clientRepository.save(client);
        
        return "redirect:/add-patient";
    }
    

    and finally add

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    

    Worked for me