0
votes

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.

3

3 Answers

1
votes

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   
    }
0
votes

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!

0
votes

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 ! :)