简体   繁体   English

并非JSP中显示的所有表单验证错误

[英]Not all of the Form validation errors shown in JSP

I have the following code for validation 我有以下代码进行验证

@RequestMapping(value = "/itemValidation.json", method = RequestMethod.POST)
    @ResponseBody
    public ValidationResponse ajaxValidation(
            @ModelAttribute(value = formName) @Valid Item item,
            BindingResult result) {
        ValidationResponse res = new ValidationResponse();
        if (!result.hasErrors()) {
            res.setStatus("SUCCESS");
        } else {
            res.setStatus("FAIL");
            List<FieldError> allErrors = result.getFieldErrors();
            List<ErrorMessage> errorMesages = new ArrayList<ErrorMessage>();
            for (FieldError objectError : allErrors) {
                errorMesages.add(new ErrorMessage(objectError.getField(),
                        objectError.getDefaultMessage()));
            }
            res.setErrorMessageList(errorMesages);
        }
        return res;
    }

Upon validation there are three elements which do not satisfy the constraints as shown below: 验证后,存在三个不满足约束的元素,如下所示:

FTIR生物识别

The problem is on the JSP only the last two of the errors are shown. 问题是在JSP上仅显示了最后两个错误。 The error with fieldName : itemPK.name is not shown. 没有显示fieldName错误: itemPK.name

I use the below code to show the errors: 我使用以下代码来显示错误:

<span class="help-inline"><form:errors path="${name}" /></span>

My generated input elements in sequence: 我按顺序生成的输入元素:

<input id="itemPK.name_id" name="itemPK.name" type="text" value="">
<input id="price_id" name="price" type="number" value="">
<input id="point_id" name="point" type="number" value="">

Not sure what went wrong, hope anyone can shed some light on this. 不知道出了什么问题,希望任何人都能对此有所了解。

If you said that last 2 field errors are displayed then you are probably have wrong name of the first field. 如果您说显示了最后2个字段错误,那么您可能是第一个字段的名称错误。 Use 使用

<form:input path="name" /><span class="help-inline"><form:errors path="name" /></span> 

or use the path value itemPK.name that is reflect to your field name in the debug window. 或使用反映在调试窗口中的字段名称的路径值itemPK.name

I found out what the problem was . 我发现了问题所在。 There's another layer where an ajax response method suppose to append all the error messages to the appropriate fields. 在另一层,ajax响应方法假定将所有错误消息附加到适当的字段。 The first error was never shown due to the jQuery selector unable to locate element with name itemPK.name , the fix was to change it to itemPK\\\\.name . 由于jQuery选择器无法找到名称为itemPK.name元素,所以从未显示第一个错误,解决方法是将其更改为itemPK\\\\.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM