0

Well, I have a Java EE application.

I'm using hibernate, jsf and primefaces.

My question is: How to output error message when a field is left empty ?

For example, in the form bellow I have the fields CNPJ and CPF:

enter image description here

How could I show a message after leaving those fields ?

A message like: "The CPF field is empty", but it should not avoid the costumer to save the data.

Ta-ta for now.

Omar
  • 1,430
  • 1
  • 14
  • 31
Gabriel Lidenor
  • 2,905
  • 2
  • 25
  • 26

1 Answers1

1

I think if you want to let user save the data and just show the message than you need to manually add error message about it. More info about manual adding error messages in the code can be found in here how to manually add error message in jsf.

You can do manual check of the fields in backing bean or write fake jsf validator that will not throw validation exception but just do the check for empty and add error message. That way you will be able to plug such validator to any field you want without additional code in backing beans but this little bit brakes the concept of validation if still lets user save the data.

Below is just example of such fake validator. If you want to attach message to the field you will need to get it's clientId and pass it instead of null when adding new FacesMessage.

public class FakeValidator implements Validator{

public void validate(FacesContext context, UIComponent component,
        Object value) throws ValidatorException {
    //... do the check you need
    context.addMessage( null, new FacesMessage( "The field is empty" );
    }
}
Community
  • 1
  • 1
trims
  • 453
  • 4
  • 9