How can a custom-validator know which commandButton was clicked

浪尽此生 提交于 2019-12-06 08:13:53

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

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.

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