How can a custom-validator know which commandButton was clicked

此生再无相见时 提交于 2020-01-02 10:26:08

问题


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?


回答1:


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.
}



回答2:


For JSF there will be inbuilt validation messages which will get displayed during Errors..or you can use Validation attributes like "validator & validatorMessages " in primefaces in their respective Tags.



来源:https://stackoverflow.com/questions/10428899/how-can-a-custom-validator-know-which-commandbutton-was-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!