JSF custom message for validation error - validation

I'm using GlassFish 3.1.2.2 and I want to define a generic validation message template to not include field identifier/label.
I've found couple of related topics on StackOverflow but none of these solutions (all involving javax.faces.validator.BeanValidator.MESSAGE) is working.
1) placing ValidationMessages.properties in root of classpath (in my case it was placed in WEB-INF/classes of my WAR file) - no effect
2) defining the ValidationMessages.properties file in <message-bundle> in faces-config.xml - no effect
3) defining the ValidationMessages.properties file in <resource-bundle> in faces-config.xml - no effect
I tested that resource bundle is correctly working as I can use it in EL, example: #{text['javax.faces.validator.BeanValidator.MESSAGE']} where text is my var from resource-bundle definition.
NOTE: I don't want to give validatorMessage attribute on each of input fields in my application. I just want to setup my custom message once for whole application.
IMPORTANT: the solution presented here Mkyong.com is not working as well.
NOTE: I'm declaring to use JSF 2.0 and Glassfish 3.1.2.2 certainly supports that.
NOTE: I don't want to implement validation in managed bean instead of using JSF validation/Bean Validation.

I'm answering myself: to set custom message for required-field validation you need to set following properties in messages bundle file (message bundle is defined in faces-config.xml by <message-bundle> tag):
1) javax.faces.component.UIInput.REQUIRED_detail - a message that is by default presented in h:message or h:messages tags - or when showDetail=true (it is by default). GENERALLY IT'S ENOUGH.
2) javax.faces.component.UIInput.REQUIRED - a message that is presented when showSummary=true in h:message and h:messages tags. Set this additionally if you display summary in your h:message/h:messages.
NOTE: this is not true that javax.faces.validator.BeanValidator.MESSAGE is responsible for required-field-error messages.

Add <h:messages> in your jsp/xhtml
and to display error message from Managed Bean depending upon condition
Use
FacesContext ctx = FacesContext.getCurrentInstance();
FacesMessage msg =new FacesMessage(FacesMessage.SEVERITY_INFO, errorMessage,detailMessage);
ctx.addMessage(null, msg);

Related

fallback messege file in spring boot is always "en"

I configured my web application as indicated in https://www.baeldung.com/spring-boot-internationalization#localeresolver
with setting Locale.ITALIAN as default locale for LocaleResolver bean.
I have two message files:
message.properties with italian messages
message_en.properties with messages in english
However, labels defined in messages_en.properties, when exists. For example with setting locale via lang=es request parameter, messages in english are shown.
The expected behaviour, if I understand, should be that if lang=en, message_en.properties should be used, where as for all other languages messages in message.properties should be used.
Suggestions?
If you use the latest version of Spring Boot (2.5.3 at the moment), the tutorial isn't as up-to-date. For example, with the latest Spring you must do additional, but simple config to override LocaleResolver bean.
Depending of your implementation, you may need to add in the application.properties file the line spring.messages.fallback-to-system-locale=false or if you overridden the "messageSource" bean, you must set messageSource.setFallbackToSystemLocale(false); in your own bean.
This way the app should work as expected, with all languages except EN using message.properties.

Customize spring boot validation message from database

I am using spring validation to validate the Rest Controller input, I would appreciate if any one can tell me is there a possibility of throwing custom message in case of exception and the custom message should come from database rather than from a properties file.
You can achieve this by setting a custom validation message source on Spring's LocalValidatorFactoryBean.
Your message source needs to implement the MessageSource interface and read its messages from the database.
Reference:
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.html#setValidationMessageSource-org.springframework.context.MessageSource-
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/MessageSource.html

Custom error message for custom validation annotation

I got this problem, that I got from my friend a annotation code that checks if input data is proper VIN number. Annotation works fine, however when number is wrong it should render a default message:
String message() default "{validator.nrVINBad}";
I dont't know where to put this message, I tried my *.properties files but it's not that. Everywhere I look, there's always instructions how to make message from *.properties files, but as I know now, you can't put in those files error messages.
This is not part of JSF. This is part of Bean Validation (BV, also known as "JSR303"). Both JSF and BV are part of Java EE. JSF just happens to have builtin recognition and delegation for BV.
To localize BV messages, just follow the instructions in chapter 4.3.1.1 of JSR303 specification. In a nutshell, create a ValidationMessages.properties file in the classpath root (there where your Java source code also is, so that it ultimately ends up in /WEB-INF/classes) with he following content:
validator.nrVINBad = Your message here
See also:
Internationalization in JSF, when to use message-bundle and resource-bundle?

spring 3 propertyeditor throws exception

New Problem:
I register / bind my custom property editor and get an java.lang.IllegalArgumentException - as expected.
The problem: I do not know how to create a custom error message if binding fails.
Any idea?
THX!
#InitBinder( { "playerCreationBean" } )
protected void initBinder( final WebDataBinder binder )
{
binder.registerCustomEditor(Date.class, new DatePropertyEditor());
}
axtavt is right. If you have a message bundle in your application (ie, messages.properties in your classpath, being used by a MessageSource implementation) spring can automatically use the friendly message in the bundle. The message 'typeMismatch' is just one of a number of default messages that are used by the binding framework, depending on the name of the object being bound as well as the property being bound to. You can use a debugger to inspect the errors instances after binding and find which messages are created by default when a binding exception occurs. I've found that the Spring Documentation is a bit lacking when it comes to the default message names that are generated.

JSF 2 - clearing component attributes on page load?

The real question: Is there a way to clear certain attributes for all components on an initial page load?
Background info:
In my application, I have a JSF 2.0 frontend layer that speaks to a service layer (the service layer is made up of Spring beans that get injected to the managed beans).
The service layer does its own validation, and I do the same validation in the frontend layer using my own validator classes to try and avoid code duplication somehow. These validator classes aren't JSF validators, they're just POJOs.
I'm only doing validation on an action, so in the action method, I perform validation, and only if it's valid do I call through to the service layer.
When I do my validation, I set the styleClass and title on the UIComponents using reflection (so if the UIComponent has the setStyleClass(:String) or setTitle(:String) methods, then I use them).
This works nicely, and on a validation error I see a nicely styled text box with a popup containing the error message if I hover over it. However, since the component is bound to a Session Scoped Managed Bean, it seems that these attributes stick. So if I navigate away and come back to the same page, the styleClass and title are still in the error state.
Is there a way to clear the styleClass and title attributes on each initial page load?
Thanks,
James
P.S. I'm using the action method to validate because of some issues I had before with JSF 1.2 and it's validation methods, but can't remember why... so that's why I'm using the action method to validate.
Ok, so I must use a PhaseListener, see this blog entry by BalusC and this other blog entry, that's a much better way of doing what I'm doing already - setting the styleClass manually using reflection - which gets all components with messages and highlights them... I'm gonna do the same, however think it's possible to add an attribute instead, haven't tried it yet.

Resources