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

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"/>

Related

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.

text fields are not getting updated after validation fails

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"

Displaying a message from managed bean with primefaces confirmation dialog component

in my page , i'm trying to display a confirmation dialog after clicking a button .In the confirmation dialog i used the attribute message to display it , this message is taken value after clicking the button . So i did it like that :
<p:commandButton value="Delete" update="testPlanetree" id="deleteBtn"
disabled="#{projectTestManagementMB.disable}" oncomplete="deleteConfirmation.show()"
action="#{projectTestManagementMB.testFn}"/>
<p:confirmDialog id="confirmDialog" message="#
{projectTestManagementMB.deleteConfirmationMsg}"
header="Confirming Deleting Process" severity="alert"
widgetVar="deleteConfirmation">
<p:commandButton id="confirm" value="Yes Sure" update="messages"
oncomplete="deleteConfirmation.hide()" />
<p:commandButton id="decline" value="Not Yet"
onclick="deleteConfirmation.hide()" type="button" />
</p:confirmDialog>
ProjectTestManagementMB Managed Bean :
private String deleteConfirmationMsg;//with getters and setters
public void testFn(){
deleteConfirmationMsg="do you want to delete ...";
}
The problem is that the deleteConfirmationMsg never take the value "do you want to delete ..." (is always empty)
Any idea will be appreciated
The <p:confirmDialog> has already generated its HTML representation on the very first HTTP request returning the page with the form and the dialog. It's merely hidden by CSS and is supposed to be shown/hidden by JS. When you change the confirm message afterwards in a bean action method, then it won't be reflected in the generated HTML output as long as you don't ajax-update it.
So, in order to get the changed message being reflected, you'd need to update the HTML representation of the <p:confirmDialog> in the client side before showing it in the oncomplete. You can for this use the update attribute of the command button which should show the dialog.
<p:commandButton ... update="confirmDialog testPlanetree">
try this i should works :
<p:commandButton value="Delete" update="testPlanetree" id="deleteBtn" actionListener="#
{projectTestManagementMB.testFn}"
disabled="# {projectTestManagementMB.disable}"
oncomplete="deleteConfirmation.show()" />

How to find indication of a Validation error (required="true") while doing ajax command

I have a form inside a dialog which I close by clicking on commandbutton with ajax ,
like this
<h:commandButton value="Add" action="#{myBean.addSomething(false)}"
id="add_something_id" >
<f:ajax render="#form someTable" execute="#form"
onevent="closeAddNewSomethingDialogIfSucceeded"></f:ajax>
</h:commandButton>
and here is the js code for closing the dialog
function closeAddNewSomethingDialogIfSucceeded(data) {
if(data.status === 'success') {
$("#dialog_id").dialog("close");
}
}
No problems till here...
Now I changed some of the dialog form fields into required="true" and now I want to prevent the closing of the dialog of i got validation errors...
But the ajax data.status still reaches its success state , and I can't figure out what indication of validation failure I can hook on...
any ideas?
Thanks to BalusC answer I did the following:
in JSF , added :
<h:panelGroup id="global_flag_validation_failed_render">
<h:outputText id="global_flag_validation_failed" value="true"
rendered="#{facesContext.validationFailed}"/>
</h:panelGroup>
the f:ajax was changed into
<f:ajax render="#form someTable global_flag_validation_failed_render"
and in js added the following check
if(data.status === 'success') {
if($("#global_flag_validation_failed").length === 0){
$("#dialog_id").dialog("close");
}
}
Not specifically for required="true", but you can check by #{facesContext.validationFailed} if validation has failed in general. If you combine this with checking if the button in question is pressed by #{not empty param[buttonClientId]}, then you can put it together in the rendered attribute of the <h:outputScript> as follows:
<h:commandButton id="add_something_id" binding="#{add}" value="Add" action="#{myBean.addSomething(false)}">
<f:ajax execute="#form" render="#form someTable" />
</h:commandButton>
<h:outputScript rendered="#{not empty param[add.clientId] and not facesContext.validationFailed}">
$("#dialog_id").dialog("close");
</h:outputScript>
(note that you need to make sure that the script is also re-rendered by f:ajax)
A bit hacky, but it's not possible to handle it in the onevent function as the standard JSF implementation doesn't provide any information about the validation status in the ajax response.
If you happen to use RichFaces, then you could just use EL in the oncomplete attribute of the <a4j:xxx> command button/link. They are namely evaluated on a per-request basis instead of on a per-view basis as in standard JSF and PrimeFaces:
<a4j:commandButton ... oncomplete="if (#{!facesContext.validationFailed}) $('#dialog_id').dialog('close')" />
Or if you happen to use PrimeFaces, then you could take advantage of the fact that PrimeFaces extends the ajax response with an additional args.validationFailed attribute which is injected straight in the JavaScript scope of the oncomplete attribute:
<p:commandButton ... oncomplete="if (args && !args.validationFailed) $('#dialog_id').dialog('close')" />
(note that & is been used instead of &, because & is a special character in XML/XHTML)
Or you could use the PrimeFaces' RequestContext API in the bean's action method to programmatically execute JavaScript in the rendered view.
RequestContext.getCurrentInstance().execute("$('#dialog_id').dialog('close')");
No conditional checks are necessary as the bean's action method won't be invoked anyway when the validation has failed.
Two things
1) Checking for an error in the 'onevent' function
Surely you have a message tag for the mandatory field?
<h:message id="m-my-field-id" for="my-field-id" errorClass="error-class"/>
So you can check for the error-class something like
var message = $('#m-my-field-id');
if(message.hasClass('error-class'){
//do this
}
else{
//do that
}
2) The DOM isn't up to date on success
Yes, I can see the message on the page in Firefox, yet jQuery tells me it is not there.
I have found that using the smallest possible timeout is sufficient to fix this
setTimeout(
function(){
setErrorStyle(source.attr('id'));
},
1
);
I think you should take a look at PrimeFaces' RequestContext. This would help you trigger client-side code on the server side.
#BalusC
in your example code the clientId of the button is not set as a param because it is a AJAX request. So
not empty param[add.clientId] is always false.
But this works:
param['javax.faces.source'] eq add.clientId
(tested with jsf-impl-2.2.12.redhat-1)
regards

Resources