简体   繁体   English

处理表示层中的JSF验证错误,避免使用h:message

[英]Handle JSF validation errors in presentation layer avoiding h:message

Is there a way to let JSF do validations for my inputs but let me control how to display the validation errors? 有没有办法让JSF对输入进行验证,但让我控制如何显示验证错误? I want to avoid using h:message 我想避免使用h:message

I find it making my code too messy when I write a h:message component below every input components inside forms needing validation support. 当我在需要验证支持的表单中的每个输入组件下面编写一个h:message组件时,发现它使我的代码太乱了。 Since there are several forms on the page using h:messages is not an option as that would show the error messages of other forms as well. 由于页面上有几种形式,因此无法使用h:messages因为它还会显示其他形式的错误消息。 So I want to handle the error messages sent after validation to the presentation layer & do the errors presentation task myself using JS/Jquery. 因此,我想处理验证后发送到表示层的错误消息,并亲自使用JS / Jquery执行错误表示任务。 So how do I handle errors thrown by JSF validation service? 那么,如何处理JSF验证服务引发的错误?

Just conditionally render the <h:messages> based on UIForm#isSubmitted() . 只是有条件地呈现<h:messages>基于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>

Or just bring in some ajax magic and update the current form only instead of the entire view. 或者只是引入一些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>

So I want to handle the error messages sent after validation to the presentation layer & do the errors presentation task myself using JS/Jquery. 所以我想处理验证后发送给表示层的错误信息和使用JS / jQuery的做了错误的演示任务喽。

Create a custom component or renderer which replaces the <h:messages> . 创建一个替换<h:messages>自定义组件或渲染器

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

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