Omnifaces validateBean:无法使showMessageFor服装正常工作 [英] Omnifaces validateBean: can not get showMessageFor attirbute to work
问题描述
我正在使用 Omnifaces <o:validateBean />
在类级别约束下使用JSR303 bean验证.我的问题是基于以前关于如何将消息附加到特定组件的问题.从Omnifaces 2.6开始,有一个showMessageFor
属性可以完全解决我当时的问题(非常感谢Omnifaces库的开发人员;-).我的问题是我无法使用它.
这是我的设置.我有三个输入字段,其中两个在类级别约束中耦合(weddingDay必须小于silverWeddingAnniversary;如有必要,我可以附加更多代码).
@DayIsBeforeAnniversary
@Entity
public class Person implements Serializable
{
private String name;
@Temporal(TemporalType.DATE)
private Date weddingDay;
@Temporal(TemporalType.DATE)
private Date silverWeddingAnniversary;
...
}
<h:form id="main_form">
<p:messages />
<p:outputLabel value="Name" for="id_name" />
<p:message for="id_name" />
<p:inputText id="id_name" value="#{personController.person.name}" /><br />
<p:outputLabel value="Wedding Day" for="id_wedding" />
<p:message for="id_wedding" />
<p:calendar id="id_wedding" value="#{personController.person.weddingDay}" /><br />
<p:outputLabel value="Silver Wedding Anniversary" for="id_anniversary" />
<p:message for="id_anniversary" />
<p:calendar id="id_anniversary" value="#{personController.person.silverWeddingAnniversary}" /><br/>
<p:commandButton
value="test"
action="#{personController.navigate()}"
update="@all"/>
<o:validateBean
value="#{personController.person}"
showMessageFor="id_wedding id_anniversary" />
</h:form>
所以我的目的是在与我指定了ID(showMessageFor="id_wedding id_anniversary"
)的两个<p:calendar/>
标签相对应的<p:message/>
标签中显示验证消息.
实际结果有所不同.常规<p:messages/>
标记中显示两个消息.并且所有输入字段都以红色突出显示(甚至是名称的输入;每个输入和标签都具有class ="ui-state-error").实际上,当我完全忽略showMessageFor属性时,没有什么区别(除了<p:messages/>
中仅呈现一条消息).
那么我该怎么做才错呢?如何指示错误消息出现在两个特定的<p:message/>
标记中?如何防止无关名的输入字段变成红色?
(在Glassfish 4.1.1上使用Omnifaces 2.6.8)
问题是2折.
-
showMessageFor
属性无意中将客户端ID解释为完整的客户端ID.下面的方法将为您工作.<o:validateBean value="#{personController.person}" showMessageFor="main_form:id_wedding main_form:id_anniversary" />
-
showMessageFor
不用于仅将特定输入字段标记为无效.不幸的是,除了获取<o:validateOrder>
之外,没有其他解决方法.<o:validateOrder components="id_wedding id_anniversary" message="You cannot celebrate wedding anniversary before being married" showMessageFor="@all" />
两个问题均已根据 issue 446 修复. >
I am using Omnifaces <o:validateBean />
to use JSR303 bean validation with a class level constraint. My question is based on a former question about how to attach message to specific components. Since Omnifaces 2.6 there is a showMessageFor
attribute which solves my question at that time completely (so big thanks to the developers of Omnifaces library ;-). My problem is that I can not get it to work.
Here is my setting. I have three input fields, two of them are coupled in a class level constraint (weddingDay must be smaller than silverWeddingAnniversary; I could attach more code if necessary).
@DayIsBeforeAnniversary
@Entity
public class Person implements Serializable
{
private String name;
@Temporal(TemporalType.DATE)
private Date weddingDay;
@Temporal(TemporalType.DATE)
private Date silverWeddingAnniversary;
...
}
<h:form id="main_form">
<p:messages />
<p:outputLabel value="Name" for="id_name" />
<p:message for="id_name" />
<p:inputText id="id_name" value="#{personController.person.name}" /><br />
<p:outputLabel value="Wedding Day" for="id_wedding" />
<p:message for="id_wedding" />
<p:calendar id="id_wedding" value="#{personController.person.weddingDay}" /><br />
<p:outputLabel value="Silver Wedding Anniversary" for="id_anniversary" />
<p:message for="id_anniversary" />
<p:calendar id="id_anniversary" value="#{personController.person.silverWeddingAnniversary}" /><br/>
<p:commandButton
value="test"
action="#{personController.navigate()}"
update="@all"/>
<o:validateBean
value="#{personController.person}"
showMessageFor="id_wedding id_anniversary" />
</h:form>
So my intention is to show the validation message in the <p:message/>
tags corresponding to the two <p:calendar/>
tags of which I specified the ID (showMessageFor="id_wedding id_anniversary"
).
The actual outcome is a bit different. Two messages show up in the general <p:messages/>
tag. And all input fields are red highlighted (even the input for the name; every input and label has class="ui-state-error"). Actually there is not much difference when I leave out the showMessageFor attribute completely (except there is only one message rendered in the <p:messages/>
).
So what am I doing wrong respectivly how can I tell the error messages to appear in the two specific <p:message/>
tags and how can I prevent the input field for the uninvolved name to become red?
(using Omnifaces 2.6.8 on Glassfish 4.1.1)
The problem is 2-fold.
The
showMessageFor
attribute unintentionally interprets the client ID as the full client ID. Below approach will work for you.<o:validateBean value="#{personController.person}" showMessageFor="main_form:id_wedding main_form:id_anniversary" />
The
showMessageFor
isn't used to mark only specific input fields as invalid. Unfortunately, there is no work around for that other than grabbing<o:validateOrder>
instead.<o:validateOrder components="id_wedding id_anniversary" message="You cannot celebrate wedding anniversary before being married" showMessageFor="@all" />
Both issues are fixed in 2.6.9 as per issue 446.
这篇关于Omnifaces validateBean:无法使showMessageFor服装正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!