sp00m
sp00m

Reputation: 48807

How to change the default "Validation Error: Value is not valid" message?

Below are 4 related posts I could find on Stack Overflow:

In these posts, the posters didn't know why they had this error message. In my case, I do know why I have this error, and I would like to change the default "Validation Error: Value is not valid" message to let's say "This is my message". How could I achieve this?

PS1: I already tried the requiredMessage, validatorMessage and converterMessage attributes, but none of them get called in this special case.

PS2: I'm using RichFaces 4.1.0, so the drop-down list I'm using is the rich:select one.


Scenario:

  1. I have two entities, for example employers and employees.
  2. I create the employee employee1.
  3. I want to create the employer employer1, and link it to the employee1 via a drop-down list.
  4. Before submitting the employer creation form, I delete the employee1 from the database.
  5. Then, I submit the employer creation form: the said message appears next to the drop-down list, since the employee1 isn't available anymore.

This behavior is the one I expected, but I just would like to change the default message to another one, more user-friendly.

Upvotes: 2

Views: 2005

Answers (2)

kolossus
kolossus

Reputation: 20691

All stock JSF messages are in the javax.faces package of your JSF Distribution. The one you're looking for is with the key javax.faces.component.UISelectone.INVALID(or UISelectMany). Modify this property to get the results you want

Upvotes: 2

BalusC
BalusC

Reputation: 1108632

Supply a custom converter and throw it as converter exception in getAsObject().

Basically,

@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
    if (submittedValue == null || submittedValue.isEmpty()) {
        return null;
    }

    Employee employee = findEmployeeInDatabase(submittedValue);

    if (employee == null) {
        throw new ConverterException("Sorry, unknown employee.");
    }
    else {
        return employee;
    }
}

Note that this also enables you to use converterMessage.

Upvotes: 2

Related Questions