Search code examples
jsf-2primefacesmyfaces

How can a custom-validator know which commandButton was clicked


my form has several "submit" buttons, and the validation of some of the fields depends on which was pressed. How can I find that out in my custom validator?


Solution

  • The button's client ID get also generated as name of the <input type="submit">. The name=value of the pressed <input type="submit"> get also sent as request parameters. So you could just check for that in the request parameter map.

    E.g.

    <h:form id="formId">
        ...
        <h:commandButton id="button1" ... />
        <h:commandButton id="button2" ... />
    </h:form>
    

    with the following in validate() implementation:

    Map<String, String> params = context.getExternalContext().getRequestParameterMap();
    
    if (params.containsKey("formId:button1")) {
        // Button 1 is pressed.
    }
    else if (params.containsKey("formId:button2")) {
        // Button 2 is pressed.
    }