JSF验证器方法导致重复的FacesMessage显示在p:messages中 [英] JSF validator method results in duplicate FacesMessage shown in p:messages
问题描述
我有两个PrimeFaces p:calendar
组件,一个代表开始日期,另一个代表结束日期.
I have two PrimeFaces p:calendar
components, one representing a begin and the other representing an end date.
我想在其中任何一个更改时立即运行一个验证器方法.
I want to run a validator method now whenever one of them changes.
以下是结束日期部分:
<p:calendar id="end-date"
widgetVar="testEndDate"
value="#{testManager.endDate}"
mindate="#{testManager.minEndDate}"
maxdate="#{testManager.maxEndDate}"
validator="#{testManager.validateEndDate}">
<p:ajax event="dateSelect" update="begin-date msgs" global="false" />
</p:calendar>
这是验证器方法代码:
@Override
public void validateEndDate(FacesContext context, UIComponent component, Object value)throws ValidatorException
{
System.out.println(this.getClass().getSimpleName() + ".validateEndDate()!");
Date beginDate = this.getBeginDate();
Date endDate = (Date)value;
if ( endDate != null && endDate.getTime() <= beginDate.getTime() )
{
System.out.println("End date less than or equal to begin date!");
// this.setValidated( false );
//
// throw new ValidatorException( JsfUtil.getValidationErrorFacesMessage( "common.validator.validTo.message" ) );
context.addMessage(null, JsfUtil.getValidationErrorFacesMessage("common.validator.validTo.message"));
}
List < FacesMessage > messages = context.getMessageList();
if ( !messages.isEmpty() )
{
System.out.println("Faces messages list not empty! Size = " + messages.size());
this.setValidated(false);
throw new ValidatorException(messages);
}
this.setValidated(true);
}
这就是我要做的:
- 通过命令按钮显示对话框.
- 点击空白的结束日期日历输入,弹出日历.
- 单击22(与开始日期相同).
- Show the dialog via command button.
- Click into the empty end date calendar input, which pops up the calendar.
- Click on the 22 (same date as the begin date).
至少从控制台输出来看,代码基本上是有效的:
The code basically works, at least according to the console output:
INFO: DefaultValidatingBeginEndDateChecker.validateEndDate()!
INFO: End date less than or equal to begin date!
INFO: Faces messages list not empty! Size = 1
但是,这在用户界面上显示了相同的FacesMessage
两次:
However, this shows the same FacesMessage
twice on the UI:
如您所见,输出与验证器方法所说的不符.
As you can see, the output doesn't match what the validator method is saying.
有人知道发生了什么/错误以及如何解决此问题? (还请注意注释过的代码显示了有效的解决方案,但是我需要知道为什么它不起作用,因为我还有另一个更复杂的验证器,该验证器具有1+ ifs,应该显示多个消息...). /p>
Does anyone know what's going on/wrong and how to fix this? (Also note the outcommented code showing the working solution, but I need to know why this isn't working, because I have another more complex validator which has 1+ ifs which is supposed to show more than one message...).
推荐答案
有效的方法是将面孔消息收集在自己的列表中:
What works is to collect the faces messages in an own list:
List<FacesMessage> messages = new ArrayList<FacesMessage>();
if ( endDate != null && endDate.getTime() <= beginDate.getTime() )
{
System.out.println( "End date less than or equal to begin date!" );
// context.addMessage( null, JsfUtil.getValidationErrorFacesMessage( "common.validator.validTo.message" ) );
messages.add( JsfUtil.getValidationErrorFacesMessage( "common.validator.validTo.message" ) );
}
// ... more ifs
if ( !messages.isEmpty() )
{
System.out.println( "Faces messages list not empty! Size = " + messages.size() );
this.setValidated( false );
throw new ValidatorException( messages );
}
this.setValidated( true );
Doh.
这篇关于JSF验证器方法导致重复的FacesMessage显示在p:messages中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!