text fields are not getting updated after validation fails - jsf-2

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>

The Problem
It's the classic jsf-2 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"

Related

Is there a way to disable validations on ADF page when clicking on a <af:button>?

I have tried setting the required attribute as follows in my input text:
required="#{ param['form:save'] == null}"
but the validation for required attribute are still being run .
I saw a few blogs where f:params are being used but the same cannot be set on the af:button. What other approach can be used here?
My save button code is:
<af:button actionListener="#{invokeActionBean.setAction}" id="save" textAndAccessKey="Save" styleClass="secondary-button" action="#{invokeActionBean.action}" partialSubmit="false" rendered="#{actionAvailable.updateAvailable}">
<f:attribute name="ACTION_MENU_ITEM" value="bindings.update" />
</af:button>
You set the immediate property of the button to true.
<af:button ... immediate="true"/>

OmniFaces highlight does not set focus with <p:ajax>

I got a simple login form. I am using <p:ajax> to invoke a <p:blockUI> (see this question).
<h:commandButton id="anmeldung_button"
action="#{benutzerAnmeldung.anmelden}" value="Anmelden"
styleClass="btn btn-info btn-sm">
<p:ajax process="#form" update="#form"/>
</h:commandButton>
<p:blockUI id="block" block=":anmeldung" trigger="anmeldung_button" />
I am using <o:highlight /> to highlight invalid inputs. This works fine.
It is working with a <f:ajax> perfectly, too.
Apperently, it is not working with <p:ajax>.
How can I achieve this?
This is caused by <p:ajax> trying to refocus the active element after executing the oncomplete scripts via doEval() (as can be seen in handleReFocus() function in core.ajax.js script. However, when you submit the form by clicking the command button instead of pressing Enter while inside the input, then that active element becomes the command button itself, not the input text. You can confirm this by just using Enter key to submit the form. You'll see that the focus is done right.
There are in your particular case 2 ways to workaround this:
Make use of PrimeFaces' own autofocus facility via <p:focus> which is placed inside the form as below:
<h:form id="anmeldung">
<p:focus context="anmeldung" />
...
</h:form>
It also automatically takes into account invalidated input fields.
Set PrimeFaces.customFocus to true in JavaScript, so that handleReFocus() function won't do its job.
<script>PrimeFaces.customFocus = true;</script>

ajax not called when h:inputText is empty

I have an input Text box and 1 button on my screen. I want to enable button only when input Text has some value in it. I have implemented following code and it is working OK when i enter some value and it enables the button. But ajax call is not made when i remove string from input Text and hence button is enabled even when i do not have any value.
code that i tried is
<h:inputText id="inputText" value="#{bean.value1}" label="Value1" required="true"
requiredMessage="value is empty" class="form-control">
<f:ajax event="keyup" render="clearButton"></f:ajax>
</h:inputText>
try this code without the required true
<h:inputText id="inputText" value="#{bean.value1}" label="Value1" class="form-control">
<f:ajax event="keyup" render="clearButton"></f:ajax>
</h:inputText>
If this solved your problem you can see more about in http://docs.oracle.com/javaee/1.4/tutorial/doc/JSFIntro10.html
It's a problem with the life circle of JSF, when you set the required like true and the input have no value your method never will be called.
Also you can try put the tag immediate for ignore the validation phase.

Why does required="true" not fail validation when disabled="true" is specified

<a4j:outputPanel id="tapalSectionSendToPanel" ajaxsingle="true">
<h:inputText id="sendToId1" value="#{MainBean.SectionBean.sendTo}"
class="createresizedTextbox"
required="true" requiredMessage="#{msg.labl_required}"
disabled="true" />
<h:message for="sendToId1" style="color:red" />
</a4j:outputPanel>
i need to validate textbox for empty validation and should show required when i click button without entering any value in textbox. It works fine without disabled="true". Whats the alternative for my requirement.
First, required and disabled don't go well together, because they are mutually exclusive as per the JSF Spec:
required: Flag indicating that the user is required to provide a submitted value for this input component.
disabled: Flag indicating that this element must never receive focus or be included in a subsequent submit.
Like I said in the comments, you can just display a message when the user tries to submit the form without selecting a node:
<h:inputText id="sendToId1" value="#{MainBean.SectionBean.sendTo}"
styleClass="createresizedTextbox" required="true" readonly="true" />
<h:message for="sendToId1" value="#{msg.labl_required}"
rendered="#{facesContext.postback and facesContext.validationFailed}" />
As an alternative you can just display a text anywhere in your markup:
<h:outputText value="#{msg.labl_required}"
rendered="#{empty MainBean.SectionBean.sendTo}" />
disabled="true" disables the input (so it's skipped when the form is submitted), if you don't want the user to type in it use readonly="readonly"

JSF 2: Cannot choose default entry in dropdown element when dropdown is required and default entry is null

I have the following JSF 2 code:
<p:selectOneMenu id="dropdown" value="#{data.selection}" required="true" converter="selectOneMenuConverter">
<f:selectItem itemLabel="Select one..." itemValue="" noSelectionOption="true" />
<f:selectItems value="#{data.entries}" var="entry" itemLabel="#{entry.name}" itemValue="#{entry}" />
<p:ajax update="display" event="change" />
</p:selectOneMenu>
<h:panelGroup id="display">
<h:outputText value="#{data.selection}" />
</h:panelGroup>
Everything works as expected when I choose a value from the dropdown.
When the user "deselects" an entry by choosing "Select One", JSF complains that this is not possible because the selectonemenu is required.
The problem comes from there that the p:ajax makes a partial submit that triggers validation. Immediate=true does also not work because in case the immediate happens on an input field (like selectonemenu is) a validation is performed.
The validation shall only happen when the user presses the "go on" button on the bottom of the page (not shown in code)
Further the given converter converts the Strings to Objects and for the default value it returns null (that's also the expected value within the domain for "no selection").
So my question is what I must do to fulfill my case.
For my this is a standard case and I cannot imagine that there is no solution for this.
Any ideas?
Best regards,
Florian
The validation shall only happen when the user presses the "go on" button on the bottom of the page (not shown in code)
Then just tell the dropdown's required attribute to do exactly that instead of hardcoding a true.
<h:form id="form">
<p:selectOneMenu ... required="#{not empty param['form:go']}">
...
</p:selectOneMenu>
...
<p:commandButton id="go" ... />
</h:form>
The #{not empty param['form:go']} will only evaluate true when the form submit has actually been taken place by the submit button which has the client ID form:go in the particular example. If you don't like hardcoding client IDs either, then reference it as follows:
<h:form>
<p:selectOneMenu ... required="#{not empty param[go.clientId]}">
...
</p:selectOneMenu>
...
<p:commandButton binding="#{go}" ... />
</h:form>

Resources