如何在各自的输入组件旁边显示 bean 验证错误

问题描述

在这主题上发现了许多类似的主题,但我有一个独特的情况,可能需要不同的解决方案。我已经从 JSF 页面删除了所有验证规则,并在 web.xml 中完全禁用了 bean 验证。现在 bean 验证的责任在于 JPA(我从

ReactSelect

<context-param>
        <param-name>javax.faces.validator.disABLE_DEFAULT_BEAN_VALIDATOR</param-name>
        <param-value>true</param-value>
    </context-param>

当 JPA 在验证错误调用我的自定义异常处理程序时,它会从异常中提取错误消息并将其存储在 Flash(FacesContext 的一个组件)中。 ConstraintViolationException 非常强大,它提供了确定导致问题的实体类属性名称及其值的方法。以下是 Flash 中由 3 个不同输入组件引起的示例错误消息:

Company name is required The bean class causing the error: org.me.mavenlistservicedb.entity.User the class field causing the error: company the value causing the error: null error template: Company name is required error detail: ConstraintViolationImpl{interpolatedMessage='Company name is required',propertyPath=company,rootBeanClass=class org.me.listservicedb.entity.User,messageTemplate='Company name is required'} Last name is required The bean class causing the error: org.me.listservicedb.entity.User the class field causing the error: lastName the value causing the error: null error template: Last name is required error detail: ConstraintViolationImpl{interpolatedMessage='Last name is required',propertyPath=lastName,messageTemplate='Last name is required'}
Date of birth is required The bean class causing the error: org.me.listservicedb.entity.User the class field causing the error: dateOfBirth the value causing the error: null error template: Date of birth is required error detail: ConstraintViolationImpl{interpolatedMessage='Date of birth is required',propertyPath=dateOfBirth,rootBeanClass=class org.me.mavenlistservicedb.entity.User,messageTemplate='Date of birth is required'}

在从 JSF 页面删除验证规则并在 JSF 中禁用 bean 验证之前,每个输入组件都将在其关联的 h:message 字段中显示错误支持 bean 还会通过 FacesMessage 将错误定向到每个输入组件的 h:message 字段。这是一个例子

<h:outputLabel id="addcompanyabel"
                                   for="addcompanyinput"
                                   style="color: green; font: caption; font-size: large;
                                   font-family: cursive; border-color: aquamarine"
                                   value="#{bundle.addusercompany}" />
                    <h:inputText id="addcompanyinput"
                                 tabindex="9"
                                 label="Company name "
                                 title="Enter Company Name"
                                 style="border-color: aquamarine"
                                 value="#{addUser.company}"
                                 maxlength="30"
                                 />
                    <h:message id="addcompanymessage" for="addcompanyinput"/>

目前,在 JSF 中,我从 Flash 中提取错误消息并将其显示页面底部

 <c:set target="#{flash}" property="keepMessages" value="false" />
                <div class="errorDetails"
                     style="color: red">
                    <c:if test="${not empty flash.errorDetail}">
                        Error details: <br/>
                    </c:if>
                    #{flash.errorDetails}
                </div>

但是,最终,我喜欢在每个输入组件的相应 h:message 中显示错误,我想知道是否有办法完成它?

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)