Note that there are some explanatory texts on larger screens.

plurals
  1. POConditional Submit through h:commandButton
    primarykey
    data
    text
    <p>We are using JSF 2.0 in our project. Apart from Mojara, we are using Primefaces 3.3 forsome components. we have a Requirement in our project that we HAVE to do Javascript validations (as part of client side validation) and then only the form submit should happen. The Validation done by JSF will anyway happen (at server side). But the Javascript validtions should also be there.</p> <p>This was the reason I had asked <a href="https://stackoverflow.com/questions/11516564/jsf-road-map-to-support-client-side-validations-in-future">this</a> question but got no comments.</p> <p>I am therefore implementing the JavaScript validations and this brings me to the actual problem i am facing. I have JS functions to do basic validations like "Required" fields, length check etc and calling them at appropiate time (like onblur). But if the user just leaves all the fields empty or does not go into some fields at all(thereby not triggering events like onblur) and clicks on the submit button, these fields are missed out for JS validations. So how do i conditionally submit the form after all the JS validaitions are done? Note that we have to use f:ajax for form submit.</p> <p>I tried the onsubmit function of the form. It works on FF but not on IE9. I tried the onclick function of the commandbutton. It calls the js but submits the form also.</p> <p>Few code if that helps</p> <p>JavaScript functions I am using </p> <pre><code> var validationErrorFound = false; function validateRequired(element,form){ if(null == element.value || "" == element.value){ var existingClass = element.getAttribute("class"); var newClass = existingClass + " inputError"; element.setAttribute("class", newClass); var label = document.getElementById(element.getAttribute("id") + "Lbl"); var labelArray = new Array(); var temp = label.innerText; labelArray = temp.split(":"); element.setAttribute("title", labelArray[0] + " is Required"); validationErrorFound = true; }else{ element.className = element.className.replace( /(?:^|\s)inputError(?!\S)/ , '' ); element.setAttribute("title", null); element.removeAttribute("title"); validationErrorFound = false; } } function validateAllRequired(form){ var all = document.getElementsByTagName("input"); var max=all.length; for (var i=0; i &lt; max; i++) { if(null != all[i].onblur ){ validateRequired(all[i],form); } } return validationErrorFound; } </code></pre> <p>JSF Page</p> <pre><code> &lt;h:form id="addUserDetailsForm"&gt; &lt;h:messages/&gt; &lt;h:panelGrid columns="2"&gt; &lt;p:outputLabel id="userNameLbl" for="userName" value="Username:" /&gt; &lt;p:inputText id="userName" required="true" onblur="validateRequired(this,this.form);" value="#{userList.addUser.userName}"&gt; &lt;f:param name="req" value="true"/&gt; &lt;/p:inputText&gt; &lt;p:outputPanel value="" /&gt; &lt;p:outputLabel id="firstNameLbl" for="firstName" value="First Name:" /&gt; &lt;p:inputText id="firstName" required="true" onblur="validateRequired(this,this.form);" value="#{userList.addUser.firstName}"/&gt; &lt;p:outputLabel id="lastNameLbl" for="lastName" value="Last Name:" /&gt; &lt;p:inputText id="lastName" required="true" onblur="validateRequired(this,this.form);" value="#{userList.addUser.lastName}"/&gt; &lt;h:commandButton styleClass="button" value="Add" onclick="validateAllRequired(this.form);" action="#{userList.dummyAdd}" &gt; &lt;f:ajax execute="@form" render=":mainArea :propertiesArea" update=":mainArea :propertiesArea"/&gt; &lt;/h:commandButton&gt; &lt;/h:panelGrid&gt; </code></pre> <p> </p> <p>Is there any way this can be done?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload