问题
Below are 4 related posts I could find on Stack Overflow:
- Validation Error: Value is not valid
- Validation error value is not valid
- Jsf : Validation error value is not valid for SelectOneMenu
- JSF with Enum 'Validation Error: Value is not valid'
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:
- I have two entities, for example employers and employees.
- I create the employee
employee1
. - I want to create the employer
employer1
, and link it to theemployee1
via a drop-down list. - Before submitting the employer creation form, I delete the
employee1
from the database. - 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.
回答1:
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
.
回答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
来源:https://stackoverflow.com/questions/14961342/how-to-change-the-default-validation-error-value-is-not-valid-message