如何更改默认的“验证错误:值无效".信息? [英] How to change the default "Validation Error: Value is not valid" message?
问题描述
下面是我可以在Stack Overflow上找到的4条相关文章:
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:我已经尝试过requiredMessage
,validatorMessage
和converterMessage
属性,但是在这种特殊情况下都不会调用它们.
PS1: I already tried the requiredMessage
, validatorMessage
and converterMessage
attributes, but none of them get called in this special case.
PS2:我正在使用RichFaces 4.1.0,所以我正在使用的下拉列表是rich:select
一个.
PS2: I'm using RichFaces 4.1.0, so the drop-down list I'm using is the rich:select
one.
场景:
- 我有两个实体,例如雇主和雇员.
- 我创建员工
employee1
. - 我要创建雇主
employer1
,并通过下拉列表将其链接到employee1
. - 在提交雇主创建表格之前,我从数据库中删除了
employee1
. - 然后,我提交雇主创建表格:由于
employee1
不再可用,该消息出现在下拉列表旁边.
- 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.
推荐答案
提供一个自定义转换器,并将其作为getAsObject()
中的转换器异常抛出.
Supply a custom converter and throw it as converter exception in getAsObject()
.
基本上
@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;
}
}
请注意,这也使您可以使用converterMessage
.
Note that this also enables you to use converterMessage
.
这篇关于如何更改默认的“验证错误:值无效".信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!