0

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.

This was the reason I had asked this question but got no comments.

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.

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.

Few code if that helps

JavaScript functions I am using

     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 < max; i++) {       
     if(null != all[i].onblur ){
        validateRequired(all[i],form);      
     }

}
return validationErrorFound;

     }

JSF Page

     <h:form id="addUserDetailsForm">   
<h:messages/>           



        <h:panelGrid columns="2">
            <p:outputLabel id="userNameLbl" for="userName" value="Username:" />
            <p:inputText id="userName" required="true" onblur="validateRequired(this,this.form);"
                value="#{userList.addUser.userName}">
                <f:param name="req" value="true"/>
            </p:inputText>  
            <p:outputPanel value="" />

            <p:outputLabel id="firstNameLbl" for="firstName" value="First Name:" />
            <p:inputText id="firstName" required="true" onblur="validateRequired(this,this.form);"
                value="#{userList.addUser.firstName}"/>

            <p:outputLabel id="lastNameLbl" for="lastName" value="Last Name:" />
            <p:inputText id="lastName" required="true" onblur="validateRequired(this,this.form);"
                value="#{userList.addUser.lastName}"/>


            <h:commandButton styleClass="button" value="Add" onclick="validateAllRequired(this.form);"                                      
                    action="#{userList.dummyAdd}" >
                 <f:ajax execute="@form" render=":mainArea :propertiesArea" update=":mainArea :propertiesArea"/>                                        
            </h:commandButton>  

        </h:panelGrid>

Is there any way this can be done?

Thanks

Community
  • 1
  • 1
Skyler Hays
  • 279
  • 1
  • 5
  • 14
  • problem is not clear. Why javascript when primefaces provides client side validation for mandatory and input length validations. – rags Jul 23 '12 at 05:41
  • @rags So you are saying that if we have an input field marked as required and we submit this through a p:commandButton, there wont be any server call and validation will happen at Client side only? – Skyler Hays Jul 23 '12 at 06:06
  • primefaces does not have client side validation. the servel call will happen. – damian Jul 23 '12 at 11:42
  • 2
    possible duplicate of http://stackoverflow.com/questions/6680179/client-side-validation-using-javascript-with-jsf also this related question may be useful: http://stackoverflow.com/questions/8416695/jsf-ajax-request-is-not-fired-when-combined-with-js-client-side-validation Basically, in case function validateAllRequired returns false, you have to return false from onclick. – damian Jul 23 '12 at 11:47

0 Answers0