【问题标题】:Why is my custom Validator for inner object not used to validate?为什么我的内部对象自定义验证器不用于验证?
【发布时间】:2019-09-14 21:37:27
【问题描述】:

我有一个如下所示的自定义验证器:

@Component
public class PersonFormValidator implements Validator {

    Logger logger = LoggerFactory.getLogger(com.myapp.generator.component.impl.PersonFormValidator.class);

    @Override
    public boolean supports(Class<?> clazz) {
        return Contractor.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        Contractor contractor = (Contractor) target;
        if (contractor.getContractorData().getNip() == null || contractor.getContractorData().getNip().equals("")) {
            errors.rejectValue("contractorData.nip", "empty");
        }
        logger.error(errors.toString());
    }
}

在 Thymeleaf 模板方面,它看起来像这样:

<form action="#" th:action="@{/contractor/update/{id}(id=${contractor.id})}" th:object="${contractor}" method="post">
        <ul class="form-style-1">
<li>
     <label>NIP<span class="required">*</span></label>
     <input type="text" th:field="*{contractorData.nip}" id="nip" th:value="${contractor.contractorData?.nip}" >
     <span class="error" th:if="${#fields.hasErrors('contractorData.nip')}" th:errors="*{contractorData.nip}">Generic error</span>
</li>

我的控制器如下所示:

@RequestMapping(value = "/contractor/update/{id}", method = RequestMethod.POST, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String updateContractor(@PathVariable("id") String id, @Validated @ModelAttribute("contractor") Contractor contractor, Model model, BindingResult result) {
if (result.hasErrors()) {
    logger.error("BINDING RESULT ERROR");
    return "index";
} else {
    Contractor contractorPO = contractorRepository.findById(id).get();
    ContractorData contractorData = 
    //not important code here
    contractorPO.setContractorData(contractorData);
    contractorRepository.save(contractorPO);
    model.addAttribute("contractor", contractorPO);
    return "index";
}

当然我已经注册了验证器:

@InitBinder({"invoicedata", "contractor"})
protected void initPersonFormBinder(WebDataBinder binder) {
    binder.addValidators(invoiceFormValidator, personFormValidator);
}

当我启动应用程序并转到应用程序地址时,我收到如下错误:

验证器的目标无效 [com.myapp.generator.component.impl.InvoiceFormValidator@e52be6c]: 承包商(id=5cc193e581c7dc75cfb7bcff,电子邮件=some@mail.com, 承包商数据=承包商数据(名字=姓名,姓氏=姓氏, businessName=Apple,businessLocation=华沙,nip=, regon=adgadgdagdag), invoices=[])

没有什么对我有用,我从昨天开始就在与这个问题作斗争......

我可以在我的代码中进行哪些更改以使验证生效?

【问题讨论】:

    标签: java spring spring-boot validation thymeleaf


    【解决方案1】:

    我的猜测是两个验证器都适用于两种形式:

    • 对于发票数据;应用 invoiceFormValidator 和 personFormValidator
    • 对于承包商;应用 invoiceFormValidator 和 personFormValidator

    如果您将验证器绑定在两个不同的方法中会怎样?

    @InitBinder("invoicedata")
    protected void invoiceDataBinder(WebDataBinder binder) {
        binder.setValidator(invoiceFormValidator);
    }
    
    @InitBinder("contractor")
    protected void contractorDataBinder(WebDataBinder binder) {
        binder.setValidator(personFormValidator);
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 2017-11-26
    相关资源
    最近更新 更多