1
votes

I am facing an issue after validations in JSF

Scenario - I have 2 drop downs and 2 input fields on the form. On selection of first drop down second one will populate and also the values of 2 text fields are depends on the first drop down selection.

1) So when I select some value from first drop down, second drop down and the text fields are populating with proper values. 2) when I click on submit button with out selecting the value from second drop down it is showing validation message as expected. 3) Now I changed the value of first drop down after validation error/fails. The values in text fields is not changing as required (means the text filed values are still showing old values) but the second drop down values are changing as expected.

Note:- Issue is only after validation fails.

<span class="col1 smallRightPadding marginBtmAdDiag">
<rich:select defaultLabel="Select bldng" enableManualInput="true"
id="search_bldng" 
value="#{testManagedBean.bldng.bldngID}"
required="true" requiredMessage="#{testHome.selectBldng}">
<f:selectItemsvalue="#{testManagedBean.lstBldng}" var="bldngVar"
itemValue="#{bldngVar.bldngID}" itemLabel="#{bldngVar.bldngCode}">
</f:selectItems>
<a4j:ajax execute="@this" event="selectitem"
listener="#{testManagedBean.populateFloor}" 
render="search_floor,hidSelBldng,hidSelFloor,hidSelbldngID" />
</rich:select>
<h:inputHidden id="hidSelBldng" value="#{testManagedBean.bldng.bldngCode}" />
<h:inputHidden id="hidSelbldngID" value="#{testManagedBean.bldng.bldngID}" />
</span>

<rich:select defaultLabel="Select Floor" enableManualInput="true"
id="search_floor"
  value="#{testManagedBean.floor.floorID}"
 required="true"
requiredMessage="#{testHome.selectFloor}">
<f:selectItems value="#{testManagedBean.floorLst}"
var="flr" itemValue="#{flr.floorID}"
itemLabel="#{flr.floorName}">
</f:selectItems>
</rich:select>

<h:commandButton id="goButtonId" value="#{testHome.goButton}" styleClass="saveButton">
<a4j:ajax event="click" execute="@form" oncomplete="drawLayerFromBtn();" 
 listener="#{testManagedBean.viewReport}" 
render="tableId,hidSelFloor"/>
</h:commandButton>

1
I had pasted my code aboveuser3474541
What text fields are you referring to? There's no text field in your code up here. Please put some effort into formatting your code to make it somewhat easier to read. Right now it's just a matted mess of textkolossus

1 Answers

0
votes

The Problem

It's the classic problem with resetting the local values on input fields after a validation error. After a validation failure, JSF doesn't reset the value of the fields that were caught in the previous validation error, hence the reason the you're still seeing the old values


To Solve

You basically need to reset the values on those (missing) textfields yourself. In a convenient place, maybe in populateFloor

   if(ctxt.isValidationFailed()){
       UIInput textField1 = (UIInput) FacesContext.getCurrentInstance().getViewRoot().findComponent("theTextFieldId")
       textField1.resetValue();
    }

The snippet above will execute only if the current request cycle is marked as failed, and then proceed to clear the local values of the textfield with id="textField1"