0

My requirement is to validate Ui component values using java script function. i would like to invoke backing bean method, once the validation is successful. How can i make it possible this requirement using primefaces 2.2 and java script. Here is the sample piece of code that i am using:

<p:commandButton id="saveButton" value="#{msgs['button.save.label']}"
    image="ui-icon #{msgs['button.save.icon.class']}" 
    actionListener="#{testBean.saveValues}"/>
3
  • 1
    why javascript? validation is built in, for cross validation, there is a workaround. Cross validation Commented Mar 27, 2012 at 16:50
  • Somehow jsf messages are not displaying in primefaces dialog window. I am not sure, if there is any bug in 2.2 version. Commented Mar 28, 2012 at 13:58
  • Doing validation by JavaScript instead of using JSF builtin validation is a terribly bad idea. You need to ask a question how to fix the real problem instead of asking how to get a hack/workaround to work. Commented Mar 29, 2012 at 13:50

2 Answers 2

1

If you want to stop your actionListenerfrom being executed just return false in your js function of the button

this button won't execute the actionListener

<p:commandButton id="saveButton" value="#{msgs['button.save.label']}"
image="ui-icon #{msgs['button.save.icon.class']}" 
actionListener="#{testBean.saveValues}" onclick="return false;"/>

this will...

    <p:commandButton id="saveButton" value="#{msgs['button.save.label']}"
image="ui-icon #{msgs['button.save.icon.class']}" 
actionListener="#{testBean.saveValues}" onclick="return true;"/>

instead of hard coded onclick call your js function onclick=myJsValidation() it all depends on the return value of the js function

Sign up to request clarification or add additional context in comments.

Comments

0

In error case of your saveValues:

FacesContext.getCurrentInstance().addMessage("ERROR_MESSAGE", new FacesMessage(FacesMessage.SEVERITY_INFO, "Error Message", "Error Message"));

In your xhtml, add update attribute for commandButton:

<p:growl id="growl_ID"/>
<p:commandButton value="Save" actionListener="#{testBean.saveValues}" update="growl_ID"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.