Best approach to jsf form validation

If I have many input controls on the form (for each of these input controls there are separate validators - as required, length, etc.), there is a command button that submits the form and calls the action method. The requirement is that - although the values ​​of the input control are, let's say, individually - the combination of these values ​​must be in order to process them together after the form is submitted. Where can I put the code to test them together?

1) Can I add a custom validator for the command button and test the combination together? for example, validate(FacesContext arg0, UIComponent arg1, Object value)but even then I will not have the values ​​of other input controls, except for the correct value / parameter of the command:

2) can I do a combination check in the action method and add check messages with FacesMessage?

or do you suggest any other approach?

Thank you for your time.

+3
source share
3 answers

Point 2 already answered Bojo. Just use it FacesContext#addMessage(). Customer ID nullallows you to put it in <h:messages globalOnly="true">. A fixed client identifier, such as formId:inputId, allows you to put it in <h:message for="inputId">.

Point 1 is doable; you can capture other components inside the validator method using UIViewRoot#findComponent():

UIInput otherInput = (UIInput) context.getViewRoot().findComponent("formId:otherInputId");
String value = (String) otherInput.getValue();

However, you need to post f:validatorin the last component UIInput. Placing it in a component UICommand(for example, a button) will not work.

, , JSF.

+4

.

, , , :

  • ( ; required = true, LengthValidator ..)
  • " " ​​ Bean (-)
  • ( )
  • Bean ( ).

: JSF . (. S/O )

, , JSF/Apache MyFaces, , , .

BalusC shoehorning , ( , . S/O , , - ), /, validate(). , , , .

JSF Seam <s:validateForm />, . , , JSF MyFaces/Mojara/Sun , .

+4

I have successfully used the second approach:

FacesMessage facesMessage = 
      new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
FacesContext.getCurrentInstance().addMessage(null, facesMessage);
+3
source

Source: https://habr.com/ru/post/1746874/


All Articles