0

I have a commandButton:

<h:commandButton  onclick="validate();" action="#{method.exitBtn}" value="Move File"></h:commandButton>

However, before calling the action, i want to make sure a value from my SELECT is chosen:

<select>
    <option value="Select Amount">Select Amount</option>
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
    <option value="D">D</option>
    <option value="E">E</option>
</select>

I tried adding onclick="validate();" and within my JS:

function validate() {
    alert("you clicked!");
}

However the alert is never displayed. I am using JSF 1.2

Oam Psy
  • 8,555
  • 32
  • 93
  • 157
  • possible duplicate of http://stackoverflow.com/questions/1921094/jsf-commandbutton-onclick-does-not-call-javascript-function – Paul Lo Jan 22 '15 at 13:24

1 Answers1

0

It seems that you're missing the point of JSF. JSF offers components to generate HTML output and a whole lifecycle to perform collecting submitted values, converting and validating them and setting them as bean properties. By default, a JSF bean action is already not invoked when JSF-managed validation fails.

Rewrite your form conform below basic example:

<h:form>
    <h:selectOneMenu value="#{bean.amount}" required="true">
        <f:selectItem itemValue="#{null}" itemLabel="Select Amount" />
        <f:selectItem itemValue="A" />
        <f:selectItem itemValue="B" />
        <f:selectItem itemValue="C" />
        <f:selectItem itemValue="D" />
        <f:selectItem itemValue="E" />
    </h:selectOneMenu>
    <h:commandButton value="Submit" action="#{bean.submit}" />
    <h:messages />
</h:form>

With this managed bean:

public class Bean {

    private String amount; // +getter+setter

    public void submit() {
        System.out.println("Submitted amount: " + amount);
        // ...
    }

    // ...
}

The JSF-provided required="true" attribute will validate the input component as required. If validation fails, any message will be shown in <h:message> associated with the component or a general <h:messages> one like shown in example.

No need for jQuery/JS validation which wouldn't work anyway when the client has JS disabled. If you really want to stick to plain HTML/JS, then you should reconsider why you're using JSF. See also What is the need of JSF, when UI can be achieved from CSS, HTML, JavaScript, jQuery?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Sorry, i am new to JSF. Within your example above, where itemValue is, how can i include the £ symbol? Ive tried £ – Oam Psy Jan 22 '15 at 13:49
  • I had an action of "creditLimit.exitButton" in my button, where has this gone in your example? – Oam Psy Jan 22 '15 at 13:55
  • 1
    1) Just use `` (and save/serve with UTF-8!). 2) Just substitute the `#{bean.submit}` in the example. By the way, if you're just starting with a new project, are you aware that JSF 1.x is basically dead technology as it has more than 5 years ago been upgraded to JSF 2.x? If not, are you sure you're reading up to date books/tutorials/resources on JSF? Otherwise you'll only end up in confusion because many things are done differently (read: better) in JSF 2.x. – BalusC Jan 22 '15 at 13:56
  • Would the same logic apply if i used commandLink instead of Button? Link doesnt seem to work. – Oam Psy Jan 22 '15 at 14:06
  • if i change to commandLink i get error TypeError: f is null in browser (Firefox) – Oam Psy Jan 22 '15 at 14:14
  • That's a different problem which is not related to the question currently asked. – BalusC Jan 22 '15 at 14:19
  • That's not right because it won't alert as the javascript does. Those functions execute in different runtimes. The javascript one would be executed before send a request to the server, while the answer will go thought the jsf code before execute the submit function. – Danielson Silva Feb 28 '20 at 17:38
  • @DanielsonSilva: the whole point is that JavaScript is the wrong tool for the job. It runs as you say fully in the client side and is therefore completely unreliable for the task of validating the submitted values. – BalusC Feb 28 '20 at 19:42
  • I think the question wasn't "What is the most reliable way to do a validation?" or "What is the right way for this job?". But thats ok. I found the problem in my case. – Danielson Silva Mar 03 '20 at 11:24