问题
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