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 therenderedattribute of the<h:outputScript>as follows:(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
oneventfunction 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
oncompleteattribute 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: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.validationFailedattribute which is injected straight in the JavaScript scope of theoncompleteattribute:(note that
&is been used instead of&, because&is a special character in XML/XHTML)Or you could use the PrimeFaces’
RequestContextAPI in the bean’s action method to programmatically execute JavaScript in the rendered view.No conditional checks are necessary as the bean’s action method won’t be invoked anyway when the validation has failed.