sTg
sTg

Reputation: 4424

Return false not working and action gets triggered on click of h:commandButton

I have an issue when return false is executed still the action takes place. I don't understand what the issue is.

<h:commandButton value="Deny" style="margin-left: 5px;" id="button"                     
action="#{myController.submitRequest('deny', 'return')}"
onclick="javascript:statusCheck();"/>

JS Code:

function statusCheck()
                {
                    var msgArray = [];
                    var validate = true;
                    var status=  document.getElementById('userValue:statusId');

                    if(status.value.trim() == "")
                    {
                        msgArray.push("Status required");
                        alert("check");
                        validate = false;
                    }
                    alert("after check");
                    alert(validate);
                    if(validate) {
                        return true;
                    }
                    else
                    {
                      if(msgArray.length >0)
                      {
                            alert(msgArray.join('\n'));
                            alert("test");
                            return false;
                      }
                    }   
                }

Now in the above JS function i can clearly see the alert test gets called but still the action is triggered. Please guide.

Upvotes: 2

Views: 4116

Answers (1)

Kukeltje
Kukeltje

Reputation: 12337

You should actually return something from the javascript that is inside the onclick attribute.

So changing

onclick="javascript:statusCheck()"

to

onclick="return statusCheck()"

should do the trick. I've seen before that it is (sometimes?) actually needed to return something from the statement in the onclick. The fact that the function that is called (statusCheck()) returns something does not mean this return value is also returned further to the commandButton onclick. So it always continues to call the action

Upvotes: 6

Related Questions