我注意到有人问了这个问题,但没有正确回答。
我有一个数据表,它有两列start date和end date。两者都包含 primefaces p:calendar 控件。我需要确保对于每一行,column1 中的日期不在 column2 中的日期之后。我想将此绑定到 JSF 验证框架中,但我遇到了麻烦。
我已经尝试标记数据表 rowStatePreserved="true" ,这允许我获取值,但是当它失败时仍然有问题,第一行中的所有值都会覆盖所有其他值。我做错了什么,还是应该使用完全不同的策略?
xhtml代码
<h:form>
<f:event type="postValidate" listener="#{bean.doCrossFieldValidation}"/>
<p:dataTable id="eventDaysTable" value="#{course.courseSchedules}" var="_eventDay" styleClass="compactDataTable"
>
<p:column id="eventDayStartColumn">
<f:facet name="header">
Start
</f:facet>
<p:calendar id="startDate" required="true" value="#{_eventDay.startTime}" pattern="MM/dd/yyyy hh:mm a"/>
</p:column>
<p:column id="eventDayEndColumn">
<f:facet name="header">
End
</f:facet>
<p:calendar id="endDate" required="true" value="#{_eventDay.endTime}" pattern="MM/dd/yyyy hh:mm a"/>
</p:column>
</p:dataTable>
</h:form>
验证码
public void doCrossFieldValidation(ComponentSystemEvent cse) {
UIData eventsDaysStable = (UIData) cse.getComponent().findComponent("eventDaysTable");
if (null != eventsDaysStable && eventsDaysStable.isRendered()) {
Iterator<UIComponent> startDateCalendarIterator = eventsDaysStable.findComponent("eventDayStartColumn").getChildren().iterator();
Iterator<UIComponent> endDateCalendarIterator = eventsDaysStable.findComponent("eventDayEndColumn").getChildren().iterator();
while (startDateCalendarIterator.hasNext() && endDateCalendarIterator.hasNext()) {
org.primefaces.component.calendar.Calendar startDateComponent = (org.primefaces.component.calendar.Calendar) startDateCalendarIterator.next();
org.primefaces.component.calendar.Calendar endDateComponent = (org.primefaces.component.calendar.Calendar) endDateCalendarIterator.next();
Date startDate = (Date) startDateComponent.getValue();
Date endDate = (Date) endDateComponent.getValue();
if (null != startDate && null != endDate && startDate.after(endDate)) {
eventScheduleChronologyOk = false;
startDateComponent.setValid(false);
endDateComponent.setValid(false);
}
}
if (!eventScheduleChronologyOk) {
showErrorMessage(ProductManagementMessage.PRODUCT_SCHEDULE_OUT_OF_ORDER);
}
}
}