0

I have to present one confirmation dialog on button click. The form has a field which is validated in bean.

                     <input type="text"
                        jsf:value="#{bean.userName}"/>                           
                   <button jsf:action="#{bean.deleteUser}"                                 
                        jsf:onclick="return confirm('Are you sure?')">
                        Delete User
                        <f:ajax execute="@form"
                            render="@form"                                        
                            onevent="commonEvent"/>
                    </button>

The field is manadatory in bean.

@NotEmpty()
@Email
public String getUserName() {
    //getters
}

Here the confirm dialog appears before the input field is validated. Is there any way that confirm dialog shall appear only after the validation is success.

Is it possible to write this without changing the commonEvent.

Patan
  • 17,073
  • 36
  • 124
  • 198
  • Your question is 'wrong'... You ask how to prevent a javascript client-side 'confirm' not to take place on an onclick... Answer: don't put it there. Your question should be 'How can I show a dialog for confirmation after all fields have validated' For that the answer below is 'valid', but @lametaweb should have searched for a duplicate. Many, many questions have already been asked, as is this one... See http://stackoverflow.com/questions/16795319/how-to-display-dialog-only-on-complete-of-a-successful-form-submit – Kukeltje Sep 15 '15 at 12:01
  • @Kukeltje. Thanks for the suggestion. I am using only JSF, but the related question explains about prime faces approach. – Patan Sep 15 '15 at 12:56
  • What do you think about my solution? Is it valid for your problem? – Javier Haro Sep 15 '15 at 15:29
  • @lametaweb: your solution is 'identical' to the duplicate with some small differences (besides being very specific with code examples). Your 'flag' is what in the duplicate is the 'postback/validation failed' and your 'jsf dialog' is a primefaces dialog or just like you posted a 'jsf dialog' (whatever that may be). – Kukeltje Sep 15 '15 at 16:46
  • @Patan: parts of that solution are generic... See for non PF related examples http://stackoverflow.com/questions/9617914/how-to-find-indication-of-a-validation-error-required-true-while-doing-ajax – Kukeltje Sep 15 '15 at 16:47
  • I think you are wrong. At the point `flag` attribute is checked, Invoke Application phase, "postback" will always be `true`, and "validation failed" will always be `false`. Anyway, are you happy with using a JSF dialog (A dialog made from JSF components) and a flag attribute? Or you need a Javascript dialog? – Javier Haro Sep 15 '15 at 17:26

1 Answers1

0

If you do Bean Validation validation then your validation will execute on the server, during Process Validations phase. If the email is valid you will enter Invoke Application phase and your deleteUser method will execute. You can add a flag attribute to your backing bean and look at it when enter in the deleteUser method. If it is true you save the entity, but if it is false you change it to true, don't perform the save operation, and let the JSF cycle render again the page. In the page you should write a modal dialog wich is rendered when flag is true. Then the dialog appear, you press the OK button and then, as said, the entity will save. So the solution requires you change your client confirmation dialog by a JSF dialog because the validation is server side.

Javier Haro
  • 1,255
  • 9
  • 14
  • Please search for 'duplicates' of very generic questions like the one above (at least how it should have been asked, but you interpreted it correctly)... http://stackoverflow.com/questions/16795319/how-to-display-dialog-only-on-complete-of-a-successful-form-submit – Kukeltje Sep 15 '15 at 12:02