0

JSF / PrimeFaces 3.5

I need when clicking on p:commandButton to check the validation first of all (input text required=true)

if validationFail == false then 
   Call the popup dialog from js :
else
   show requiredMessage from inputText (this field is mandatory...)

I have tried with oncomplete but it calls my bean and after the js popup dialog. I dont want it.

I need in this order : click p:button -> check validation -> if not fails -> show primefaces dialog.
if fails after validation-> render message

My xhtml :

<p:commandButton id="btnSalvar"
    value="abc"
    action="#{notaFiscalManagedBean.salvar}" 
    oncomplete="if (args.validationFailed) return true; else return showPF_DiagBox()"

in my showPF dialog I call bean method. if OK clicked by user.

Al2x
  • 1,001
  • 5
  • 26
  • 37

3 Answers3

1

It is better to user RequestContext of primefaces which allows user to execute javascript which is set from managed bean. You can use it by modifying your method at #{notaFiscalManagedBean.salvar} as shown below.

    public String salvar(){
    boolean valid=true;
    //Do your validation here
    if(valid){
          RequestContext.getCurrentInstance().execute("showPF_DiagBox()");
    }
    }

If you want to do the validation on client side before submitting the request to the server then just do the following change in your code,

<p:commandButton id="btnSalvar"
value="abc"
action="#{notaFiscalManagedBean.salvar}" 
onclick="if(validationFailed()){return false}"
oncomplete="showPF_DiagBox()"/>

Also write down a javascript function to do validations

    function validationFailed(){
    //Check various conditions based on component validations and return whether  validatoin  failed or not   
    }
Jitesh
  • 1,384
  • 10
  • 20
  • hi @Jitesh. the issue is that I need to validate jsf fields first of all and after that, open js dialog. If I click ok on dialog button, so I call for last my bean method. – Al2x Mar 13 '14 at 19:06
  • Im trying to call some dialog from requestContext...execute but nothing happens – Al2x Mar 13 '14 at 19:06
  • thats a gread idea but I have a bunch of jsf validators in my xhtml. I ll have to rewrite all of them in js ? – Al2x Mar 14 '14 at 12:45
  • The dialog called from execute() should work but as you describe in your comment i think you are using jsf "required" property for validation and you want to stop user to go to server if validation failed. But required="true" perform validation at server side. And It seems like due to failing in validation your further transaction is not working, so you need to decide where the validation you want to do. – Jitesh Mar 14 '14 at 15:35
  • I kept required=true and jsf validation and implemented in my oncomplete commandButton facesContext.validationFailed with js function. it worked like a charm. thx @Jitesh – Al2x Mar 14 '14 at 16:58
0

Try this:

<p:commandButton id="btnSalvar"
    value="abc"
    action="#{notaFiscalManagedBean.salvar}"
    update="@form"
    render="@form"/>

By adding 'render' and 'update' attributed your form will have to reload, and then process all the validations inside of it's form.

Hope this can help you, good luck!

N0nbot
  • 190
  • 1
  • no way. in my commandButton I have to call my js on click and it ignores the validation calling the js dialog – Al2x Mar 13 '14 at 17:24
  • Please, post your inputText tag that is causing this issue. – N0nbot Mar 13 '14 at 17:33
  • its a simple inputText inside h:form. it is – Al2x Mar 13 '14 at 17:35
  • Try to add a tag and see if it will help you showing a validation message. If it do, then you should think in change it's native validation message to fulfill your needs. – N0nbot Mar 13 '14 at 17:39
  • the message works fine with render and upate in commandButton but unhapply my js dialog does not render and my bean method is invoked either. – Al2x Mar 13 '14 at 17:43
0

In my oncomplete commandButton I got what I wanted getting from @BalusC answer (How to find indication of a Validation error (required="true") while doing ajax command) and @Tuukka Mustonen (JSF 2.0 AJAX: Call a bean method from javascript with jsf.ajax.request (or some other way)) and making some adjustments to fit my needs.

This way, if there are any validation errors or any converters do be validated, they are rendered at screen first of all. If there are not validation errors so I execute my js function and inside it I fire my bean method if needed.

thanks for everybody ! :)

Community
  • 1
  • 1
Al2x
  • 1,001
  • 5
  • 26
  • 37