如何更改默认的“验证错误:值无效".信息? [英] How to change the default "Validation Error: Value is not valid" message?

查看:154
本文介绍了如何更改默认的“验证错误:值无效".信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我可以在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:我已经尝试过requiredMessagevalidatorMessageconverterMessage属性,但是在这种特殊情况下都不会调用它们.

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.

场景:

  1. 我有两个实体,例如雇主和雇员.
  2. 我创建员工employee1.
  3. 我要创建雇主employer1,并通过下拉列表将其链接到employee1.
  4. 在提交雇主创建表格之前,我从数据库中删除了employee1.
  5. 然后,我提交雇主创建表格:由于employee1不再可用,该消息出现在下拉列表旁边.
  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.

推荐答案

提供一个自定义转换器,并将其作为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆