对于Ajax请求,省略对p:selectOneMenu的验证 [英] Omit validation for p:selectOneMenu, for Ajax requests
问题描述
我有一个Jsf页面,其中的片段带有<p:selectOneMenu/>
(素面)和与之关联的Ajax.它具有默认的选择项-选择一个---"以及其他动态创建的项.
I have a Jsf page with a fragment with <p:selectOneMenu/>
(Prime faces) and an Ajax associated with it. It have the default select item "--- Select One ---" and others items that were created dynamically.
此字段为必填字段,因此,如果用户未选择而提交表单,则页面将显示错误消息.
This field is required, so if users submit the form without select, the page show a error message.
当我选择其他选项之一,然后再次选择"--- Select One ---"时,就会出现问题.然后,即使在我提交表单之前,页面也会显示必填字段消息.我尝试在<p:ajax>
和<p:selectOneMenu>
标签中使用immediate
的不同方法,但是它们都不具有预期的行为.
The problem occurs when I select one of this others options and go back selecting "--- Select One ---" again. Then the page shows the required field message, even before I submit the form. I try different ways of immediate
in <p:ajax>
and <p:selectOneMenu>
tags but none of them has the expected behavior.
<p:messages>
声明如下:
-list.xhtml
-- list.xhtml
<p:messages id="messages" autoUpdate="true" closable="true" escape="false" />
<.... include fragment.xhtml ....>
和片段:
-fragment.xhtml
-- fragment.xhtml
<p:selectOneMenu id="selectTipoCarro"
value="#{carroBean.carro.tipoCarroEnum}"
required="true">
<f:selectItems value="#{carroBean.listaSelectItemTipoCarroInclusao}" />
<p:ajax update="outputInicioVigenciaWrapper outputLabelCalendarInicioVigenciaWrapper"
listener="#{carroBean.aoMudarTipoCarro}" />
</p:selectOneMenu>
<h:panelGroup id="outputLabelCalendarInicioVigenciaWrapper">
<h:outputLabel id="outputLabelCalendarInicioVigencia"
rendered="#{carroBean.edicaoDataInicioVigenciaDisponivel}"
for="calendarInicioVigencia">
<span>*
#{labels['carro.inicio.vigencia']}: </span>
<p:calendar id="calendarInicioVigencia"
value="#{carroBean.carro.dataInicioVigencia}"
showOn="button"
pattern="dd/MM/yyyy" mask="true"
required="true"/>
</h:outputLabel>
</h:panelGroup>
<h:panelGroup id="outputInicioVigenciaWrapper">
<h:outputLabel for="outputInicioVigencia"
rendered="#{not carroBean.edicaoDataInicioVigenciaDisponivel}">
<span aria-live="polite">
<h:outputText id="outputInicioVigencia"
value="#{carroBean.carro.dataInicioVigencia}"
styleClass="dataFormat"
</h:outputText>
</span>
</h:outputLabel>
</h:panelGroup>
private SelectItem obterSelectItemSelecione() {
SelectItem selectItem = new SelectItem("", "-- Select One --");
return selectItem;
}
private void preencherListaSelectItemTipoCarro(List<SelectItem> select, TipoCarroEnum[] tiposCarrosConsiderados) {
select.clear();
select.add(obterSelectItemSelecione());
for (TipoCarroEnum tipoCarro : tiposCarrosConsiderados) {
select.add(new SelectItem(tipoCarro, tipoCarro.getNome()));
}
}
public void aoMudarTipoCarro() {
getCarro().setDataInicioVigencia(carroService.obterProximaDataInicioVigenciaDisponivel(getCarro().getTipoCarroEnum()));
}
推荐答案
说明
之所以会发生这种情况,是因为您有一个p:messages
组件,且autoUpdate
设置为true,并且已将p:ajax
附加到selectOneMenu上,因此只要您的值更改,就会更新p:messages
.因此,由于使用required="true"
,选择"--- Select One ---"后将立即显示错误消息.
This happens because you have a p:messages
component with autoUpdate
set to true and you have attached p:ajax
to your selectOneMenu so anytime your value changes, p:messages
is updated. Therefore, due to the use of required="true"
an error messages is shown as soon as you have selected "--- Select One ---".
解决方案
- 将
ignoreAutoUpdate="true"
添加到您的p:ajax
或 - 如有必要,请删除
autoUpdate="true"
- Add
ignoreAutoUpdate="true"
to yourp:ajax
or - Remove
autoUpdate="true"
if not necessary
这篇关于对于Ajax请求,省略对p:selectOneMenu的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!