1

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.

Community
  • 1
  • 1
sp00m
  • 47,968
  • 31
  • 142
  • 252

2 Answers2

2

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

kolossus
  • 20,559
  • 3
  • 52
  • 104
  • Is there any way to put different messages depending on the drop-down list? For the example I gave, I could need *"The employee doesn't exist anymore"*, but for another drop-down list, I could need another message. – sp00m Feb 19 '13 at 16:05
  • @sp00m 1. Try to avoid messing with that file too much, it's tacky design. 2. The properties file doesn't hold logic, just key-value pairs, so in theory, you can store any message string there. You have to implement a validator to then hold the logic that selects the right message based on whatever condition – kolossus Feb 19 '13 at 16:09
2

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • To be honest, I hoped you would have seen my question since I bet you probably had the solution `;)` This is exactly what I was looking for, many thanks. – sp00m Feb 19 '13 at 17:36