Trigger HTML5 constraint validation before submitting a form using ajax - ajax

I have the following code snippet and the standard client side html validation is not triggerd. How can I trigger the standard validation and get the action called if valid.
<h:form>
<h:inputText type="text" value="#{myBean.value}">
<f:passThroughAttribute name="required" value="required" />
</h:inputText>
<h:commandButton value="submit" type="submit" action="#{myBean.save}">
<f:ajax execute="#form" render="#form" />
</h:commandButton>
</h:from>
Without the ajax the client side validation will be triggered and the form will not be send if the input is empty.
How can I reach this with ajax?

JSF ajax engine does not check HTML5's form.checkValidity() before proceeding the request. Essentially, you need to do that job yourself in the onclick attribute of the command button. If it returns false, then it will block the ajax action.
Please note that you should never rely on only client side validation, because that's fully controllable by the enduser. I strongly recommend to add server side validation as well. When you add ajax, you can easily make use of server side validation with the same user experience as client side validation.
See also:
JSF2 Validation Clientside or Serverside?

Related

Session get lost even if I am interacting on page

I'm working with primefaces 3.4.2 Javax.faces 2.1.9
My web.xml
<session-config>
<session-timeout>45</session-timeout>
</session-config>
<p:idleMonitor id="idMonitorSession" timeout="2400000"
onidle="dlgMensajeSessionExp.show()" />
<p:dialog header="Sesion Ended" resizable="false"
widgetVar="dlgMensajeSessionExp" width="300" height="120"
modal="true" closable="false" appendToBody="true">
<center>
<h:outputText value="Session Ended" />
<h:form id="sessionForm">
<h:commandLink actionListener="#{user.exit()}">
<h:graphicImage style="border:0px" value="/resources/images/login.png"
width="80px" />
</h:commandLink>
</h:form>
</center>
</p:dialog>
However session get lost even if i am working in the pages, do you have any idea why is this happening,
Web Server is Websphere 7.0.0.17
Interacting with the page in such a way that the server isn't invoked via ajax or a complete form post doesn't reset the timeout against the session server side. Using ajax events for the idleMonitor should do the trick.
<p:idleMonitor id="idMonitorSession" timeout="2400000"
onidle="dlgMensajeSessionExp.show()">
<p:ajax event="active"/>
</p:idleMonitor>
Edit: I noticed the "active" event doesn't fire when the mouse is moved, the user will be required to interact with the page enough to trigger this event. If it is critical to keep the server synced with the page, ajax invocations will be required more often than this event will create. You don't have to necessarily invoke a noop action on the server side, setting the event type is enough.
Another possibility is to use ajax events when the type of interactions occur that indicate your users are actively using the page. For instance, typing a long message in a text area or filtering a large data table.
<p:ajax event="filter" listener="#{bean.activeListener}"/>
Either way, the goal is to keep the server aware that the user is indeed active without flooding the server with unneeded ajax events.

How can I display the reason for <a4j:status> error?

I'm currently debugging an annoying AJAX bug in our web application using JSF2 and RichFaces v4.5.12.Final where I need further information about the reason why an ajax update fails.
We generate rather complex forms with several elements and on some of these forms the following code snippet fails to re-render and sets to error.
<h:selectBooleanCheckbox value="#{myBean.value}">
<a4j:ajax event="valueChange" execute="#form" render="#form,messages" status="minimal" />
</h:selectBooleanCheckbox>
The AJAX status is displayed using the following code:
<a4j:status name="minimal" onstart="$('body').css('cursor', 'progress');" onstop="$('body').css('cursor', 'default');">
<f:facet name="start">
<h:graphicImage name="/3rd-Party/Fugue-Icons/hourglass.png" styleClass="middle" />
</f:facet>
<f:facet name="stop"></f:facet>
<f:facet name="error">
<h:graphicImage name="/3rd-Party/Fugue-Icons/exclamation-red.png" styleClass="middle" />
</f:facet>
</a4j:status>
Is there any way to display the reason why the render fails? Can I access any variable containing some kind of error message?
Debugging shows that the execute is performed and doesn't result in any server side errors. The error happens on client side where the AJAX fails to reload the page content. It doesn't matter if we use render="#all" or anything else... the error occurs on some of our pages. Others with the same <a4j:ajax> work normally.
The error handler is triggered when there is a problem with the server response. RichFaces has <a4j:log> specifically for debugging, it will at least tell you what kind of error it is.

JSF f:ajax prompt before any action

How I can show dialog before any f:ajax action.
I have code as following:
<h:commandButton value="Create Project" styleClass="greyishBtn submitForm" action="#{projectController.delete}">
<f:ajax execute="#form" onevent="function(data) {createProjectEventHandler(data);}" render=":tblProject txtNewProjectName txtNewProjectStartDate taNewProjectDesc"/>
</h:commandButton>
This code works fine, but I need a confirm dialog before call delete action, I tried onevent, but it only have begin, success and complete events, what I need is confirm dialog before 'begin' event.
Any idea?
Thanks in advance.
Before the ajax even starts you can cancel it (ajax) or the submit action itself by using onclick="return false" in the <h:commandButton itself...
So the simplest way it to use js confirm("Delete?") dialog with onclick
<h:commandButton onclick="return confirm('Delete?');" value="Create Project" styleClass="greyishBtn submitForm" action="#{projectController.delete}">
<f:ajax execute="#form" onevent="function(data) {createProjectEventHandler(data);}" render=":tblProject txtNewProjectName txtNewProjectStartDate taNewProjectDesc"/>
</h:commandButton>
A more advanced way could be found here jQuery UI confirm dialog not returns true/false it uses jQuery dialog with JSF

How to prevent reload of page in Richfaces on action/event?

I am using JSF2.0 and RichFaces 4.0.1 Final. I want to prevent the reload of page on occurence of event.
My use case is: There is a Command button with hidden style. I need to invoke the event attached to it in certain case from Javascript.
Problem: Though I am able to invoke the hidden button, the issue is, the moment it gets invoked, the page is getting reloaded. I want to prevent this reload.
Just use ajax:
<h:commandButton id="foo" action="#{bean.foo}" styleClass="hide">
<f:ajax />
</h:commandButton>
Or, since you're already using RichFaces:
<a4j:commandButton id="foo" action="#{bean.foo}" styleClass="hide" />

valuehchange listener, ajax

I am using a JSF page in a portal environment. If I use a valueChangeListener, the method is not called in the backing bean unless I use onclick="submit()". This submits my page which I don't want. Same is the case with actionListner.
Also, if I use ajaxRefreshSubmit, my whole page is submitted rather than submitting the specified part on which the ajaxRefreshSubmit is used.
Why this behaviour? Is it because we are using it in a portal environment, or did we miss something in the configuration files?
The valueChangeListener runs indeed only when you submit the form to the server. It's indeed one of the major JSF beginner's misconceptions to think that it runs at the client side. It's not part of HTML or JavaScript code which runs in the webbrowser. It's part of Java/JSF code which runs in webserver. So the client has to send a request from webbrowser to webserver somehow. Submitting the form is one of the ways.
For the remnant, I don't have anything to say about ajaxRefreshSubmit since that's specific to the IBM component library which I don't use (and also can't because it's not open source nor freely available). If you don't get more respons here at Stackoverflow, then your best bet may be posting the question at their own forum at developerworks. It's only not as active and high-quality as here.
<h:form id="toilclaimform">
<h:panelGroup id="container">
<!-- Read only version -->
<hx:commandExButton type="button" id="testButton2" value="Submit" styleClass="hideRefreshToil" onmousedown="$.resourceManUtilities.formRefreshCookie()" >
<hx:behavior event="onclick" id="behavior3" behaviorAction="get" targetAction="container" ></hx:behavior>
</hx:commandExButton>
<hx:commandExButton onmousedown="runToilVal()" onmouseup="$.resourceManUtilities.formSubmitCookie()" type="submit" id="testButton" value="Submit" styleClass="rmButtons" action="#{toilClaimBean.submit}">
<hx:behavior event="onclick" id="behavior2" behaviorAction="get" targetAction="container" ></hx:behavior>
</hx:commandExButton>
</h:panelGroup>
</h:panelGroup>
<hx:ajaxRefreshSubmit id="ajaxRefreshRequest1" onstart="$.resourceManUtilities.ajaxLoadingStarting('form')" oncomplete="$.resourceManUtilities.ajaxFormSubmit('hideRefreshToilList')" target="container"></hx:ajaxRefreshSubmit>
</h:form>
When using this library you know longer use the standard command buttons, you instead use the hx command button, there is an example in the code above. Using this button will ensure that you don't get a full pagr refresh and will update the desired portlet.
Cheers

Resources