1

In JSF's custom convertors and validators you can say:

FacesMessage message = new FacesMessage(
    FacesMessage.SEVERITY_ERROR, "Invalid length!",
    "Length = 8");
throw new ConverterException(message);

and ti sends the message to some h:message component determined from the convertor or validator. Is there a way to do something like that in regular bean method(and if so, how to determine the h:message component witch will render the message) or the better way is to do all the validation work in custom validators and to left business logic with no checks for the bean methods.

Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145

1 Answers1

5

Of course that sometimes you cannot only count on Validators but need to do some validation in bean. (most of the times you need this kind of validation when you are validating data for multiple inputs together)

For this case, if you have an h:messages control on the page, this will show the error there:

FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage("This username already exists!"));

If you want to display validation messages per components and not using a general one per page, see this answer.

Ps: Pay attention that if you use h:messages it will also display other components validation messages also. If you do not want this set globalOnly="true"

Community
  • 1
  • 1
Cristian Boariu
  • 9,603
  • 14
  • 91
  • 162