3

I have a problem showing a dialog when I try to do a test in the oncomplete attribute.
But when I refresh the page it worked fine, and I have to refresh on each change of the variable in the backing bean. Even if I show the dialog without test it worked to. The problem is in debug mode. JSF doesn't call get when I test after the action was invoked.

<p:commandButton value="#{msg.LABEL_ADD}"
                 action="#{axeBean.calculer}"
                 style="width:100px;"
                 update="msgs"
                 oncomplete="if(#{axeBean.formvalide})  PF('dialog').show()" />

formvalide is a boolean.

How can I solve this issue?

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
mogiwara
  • 47
  • 1
  • 6
  • 1
    Possible duplicate of [How to display dialog only on complete of a successful form submit](http://stackoverflow.com/questions/16795319/how-to-display-dialog-only-on-complete-of-a-successful-form-submit) – Jasper de Vries May 05 '17 at 12:02
  • thanks man ... but this just solve when you have required msg or validation on jsf client side but when i return a message from backing thats not working clearly :) – mogiwara May 05 '17 at 14:45
  • Stick to JSF and use messages and validationFailed (you can set them from your backing bean). Don't reinvent the wheel ;-) – Jasper de Vries May 05 '17 at 14:47
  • i already do it validationFailed worked just with required and client side validation ... but for me i want to valdate samething in backing bean but not working ... it call dialog even i have a message error – mogiwara May 05 '17 at 15:48

1 Answers1

6

I still believe the core of the question should be solved by the applying the answer on the question I've marked as duplicate. As you are running into several issues, I'll give you some pointers here.

PrimeFaces oncomplete doesn't refresh the variable on test ... But when I refresh the page it worked fine.

Your button is obviously rendered before you can click on it. So the oncomplete attribute was also rendered. It is not dynamic. So it will not be reevaluated when the action is invoked.
If you need to access #{axeBean.formvalide} in some JavaScript call, use it as the value of a hidden input, and update the input after you invoke the action. Now you can get it using document.getElementById('hiddenInputsClientId').value.

See also:

I have a problem showing a dialog when I try to do a test in the oncomplete attribute.

You don't need to expose your own validationFailed. PrimeFaces will give you an args object in the oncomplete function, which contains a validationFailed property if the validation failed. So just use:

oncomplete="if (args &amp;&amp; !args.validationFailed) PF('dialog').show()"

See also:

validationFailed worked just with required and client side validation, but I want to validate something in backing...

Just do the checks you need to do in your action. If your custom validation failed, you can set a flag in the Faces context:

FacesContext.getCurrentInstance().validationFailed();

If you need to set an error message, use:

FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "message", null);
FacesContext.getCurrentInstance().addMessage("clientId", message);

Here you can use null as clientId to set a global message.

If you are using OmniFaces, you can simply use:

Faces.validationFailed();
Messages.addError("clientId", "message");

See also:

I'm not sure what the check is you are doing... It might be that you are better of by creating a custom validator. See:

Community
  • 1
  • 1
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
  • thanks so much .. the probleme is i didnt set a flag in the Faces context . after i have a validation error ... thnaks again – mogiwara May 08 '17 at 09:59