0

有没有办法让 JSF 对我的输入进行验证,但让我控制如何显示验证错误?我想避免使用h:message

h:message当我在需要验证支持的表单内的每个输入组件下方编写一个组件时,我发现它使我的代码过于混乱。由于页面上有多个表单,因此h:messages不能使用 is 选项,因为这也会显示其他表单的错误消息。所以我想处理验证后发送到表示层的错误消息并使用 JS/Jquery 自己执行错误表示任务。那么如何处理 JSF 验证服务抛出的错误呢?

4

1 回答 1

0

只需有条件地渲染<h:messages>based on UIForm#isSubmitted()

<h:form binding="#{form1}">
    <h:messages rendered="#{form1.submitted}" />
    ...
</h:form>
<h:form binding="#{form2}">
    <h:messages rendered="#{form2.submitted}" />
    ...
</h:form>
<h:form binding="#{form3}">
    <h:messages rendered="#{form3.submitted}" />
    ...
</h:form>

或者只是引入一些 ajax 魔法并仅更新当前表单而不是整个视图。

<h:form>
    <h:messages />
    ...
    <h:commandButton ...><f:ajax execute="@form" render="@form" /></h:commandButton>
</h:form>
<h:form>
    <h:messages />
    ...
    <h:commandButton ...><f:ajax execute="@form" render="@form" /></h:commandButton>
</h:form>
<h:form>
    <h:messages />
    ...
    <h:commandButton ...><f:ajax execute="@form" render="@form" /></h:commandButton>
</h:form>

所以我想处理验证后发送到表示层的错误消息并使用 JS/Jquery 自己执行错误表示任务。

创建一个自定义组件或渲染器来替换<h:messages>.

于 2012-10-04T13:32:53.780 回答