Yasothar
Yasothar

Reputation: 453

Spring Boot, Thymeleaf Validation for Composition Class

How can I validate a composition relationship in Thymeleaf/Spring Boot. I have a simple FundTrf class which "has a" Data class. Problem is when I validate form inputs, FundTrf class related fields are getting validated, but the Data class related fields are not getting validated. Is there additional biding needs to be done between these classes. Below is what I have tried.

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>HNB CEFT | Test Bed</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <h1>Form</h1>
        <form action="#" th:action="@{/ceft/fundTrf}" th:object="${fundTrf}" method="post">
            <table>
                <tr><td>Version </td><td><input type="text" th:field="*{version}" /></td>
                    <td th:if="${#fields.hasErrors('version')}" th:errors="*{version}">Version Error</td>
                </tr>
                <tr><td>Bank Code </td><td><input type="text" th:field="*{data.dest_bank_code}" /></td>
                    <td th:if="${#fields.hasErrors('data.dest_bank_code')}" th:errors="*{data.dest_bank_code}">Bank Code Error</td>
                </tr>
                <tr><td>Amount </td><td><input type="text" th:field="*{data.amount}" /></td>
                    <td th:if="${#fields.hasErrors('data.amount')}" th:errors="*{data.amount}">Amount Error</td>
                </tr>
            </table>
            <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
        </form>
    </body>
</html>

Below is my controller class.

@Controller
public class Hello  implements WebMvcConfigurer{

    @GetMapping("/ceft/welcome")
    public String welcomeForm(Model model) {
        model.addAttribute("fundTrf", new FundTrf());
        return "welcome";
    }

    @PostMapping("/ceft/fundTrf")
    public String ceftTransaction(@ModelAttribute @Valid FundTrf fundTrf,  BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return "welcome";
        } else {
            return "result";
        }
    }
}

Below is my FundTrf class

public class FundTrf {

    @NotEmpty
    private String version;
    private Data data;

    ..Getters and Setters
}

And this is the Data class.

public class Data {

    @NotEmpty
    private String reqId;
    @NotEmpty
    private String frm_hnb_account;
    @NotEmpty
    private String dest_bank_account;
    @NotEmpty
    private String benificiary_name;
    @NotEmpty
    private String dest_bank_code;
    @NotEmpty
    @Size(min = 2, max = 30)
    private String amount;

    ..Getters and Setters
}

The issue is when I submit the form with empty values the message "Version must not be empty" is coming up, but Amount validation is not working. What am I doing wrong here?

Upvotes: 1

Views: 441

Answers (1)

Ioan M
Ioan M

Reputation: 1207

You have to set @Valid on the object Data in order for your Data properties to be also validated.

public class FundTrf {

    @NotEmpty
    private String version;
    @Valid //ADDED VALID HERE
    private Data data;

    ..Getters and Setters
}

The javadoc for javax.validation.Valid says:

Marks a property, method parameter or method return type for validation cascading. Constraints defined on the object and its properties are be validated when the property, method parameter or method return type is validated. This behavior is applied recursively.

Upvotes: 1

Related Questions