I have a form with some required fields depending on a selectOneMenu
, like the following:
<p:selectOneMenu id="myList" value="#{myBean.selectedItem}">
<p:selectItems value="#{myBean.myItems}" />
<p:ajax listener="#{myBean.myList_change}"
process="myList field1 field2 field3"
update="field1 field2 field3" />
</p:selectOneMenu>
<p:inputNumber id="field1" required="true" />
<p:inputNumber id="field2" required="#{myBean.selectedItem gt 1}" />
<p:inputNumber id="field3" required="#{myBean.selectedItem gt 2}" />
The first time I push the submit button, without fill the required fields:
<p:commandButton id="mySubmit" action="#{myBean.myAction}" />
I get the validation errors, then if I change the selectOneMenu
value the required expression will no longer be evaluated.
For example, if I submit the form with selectedItem equals to 3
I get all the validation errors, then I submit the form with selectedItem equals to 1
and primefaces still requires all the three fields as mandatory.
I tried to add a resetInput to the button:
<p:resetInput target=":myForm" />
Rather than the immediate="true"
to the selectOneMenu
, without any success.
Any idea, please?
Note: The PrimeFaces version is 6.2
PS: In the image, just the first field is required, but should required all the elements.
Following the complete minimal reproducible example:
The xhtml file (test.xhtml):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Example</title>
</h:head>
<h:body bgcolor="white">
<h:form>
<p:messages showDetail="true" />
<p:selectOneMenu id="myList" value="#{myBean.selectedItem}">
<f:selectItem itemLabel="1" itemValue="1" />
<f:selectItem itemLabel="2" itemValue="2" />
<f:selectItem itemLabel="3" itemValue="3" />
<p:ajax listener="#{myBean.myList_change}"
process="myList field1 field2 field3" update="field1 field2 field3" />
</p:selectOneMenu>
<p:inputNumber id="field1" required="true" decimalPlaces="0"
value="#{myBean.field1}" />
<p:inputNumber id="field2" required="#{myBean.selectedItem gt 1}"
decimalPlaces="0" value="#{myBean.field2}" />
<p:inputNumber id="field3" required="#{myBean.selectedItem gt 2}"
decimalPlaces="0" value="#{myBean.field3}" />
<p:commandButton actionListener="#{myBean.submit}" update="@form" />
</h:form>
</h:body>
</html>
The bean (MyBean.java):
package com.mkyong.common;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "myBean")
@SessionScoped
public class MyBean implements Serializable {
private static final long serialVersionUID = 1L;
private static final String STRING_EMPTY = "";
private Long selectedItem;
private String field1, field2, field3;
public Long getSelectedItem() {
return selectedItem;
}
public void setSelectedItem(final Long selectedItem) {
this.selectedItem = selectedItem;
}
public String getField1() {
return field1;
}
public void setField1(final String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(final String field2) {
this.field2 = field2;
}
public String getField3() {
return field3;
}
public void setField3(final String field3) {
this.field3 = field3;
}
public void myList_change() {
final long value = selectedItem.longValue();
if (value < 1) {
setField1(STRING_EMPTY);
}
if (value < 2) {
setField2(STRING_EMPTY);
}
if (value < 3) {
setField3(STRING_EMPTY);
}
}
public void submit() {
}
}
In the web.xml you should set the welcome-page:
<welcome-file-list>
<welcome-file>faces/test.xhtml</welcome-file>
</welcome-file-list>