How can I display custom validation message in using prime faces message tag?
Suppose I have a field called Description
, I want to get message like this:
Description:Plese enter value.
here Description is field name,So my real requirement is
formId:inputId:Plese enter value.
but currently I am getting this:
Description: Validation Error: Value is required.
My code:
<p:messages id="message" />
<p:inputText id="updatedescription" value="#{equipTemplateBean.equipmentSpecificationsVO.description}" requiredMessage="Please enter Description" required='true'
</p:inputText>
Please suggest how to do this.
Please consider my minimal example displaying the custom error message "This field is required." when submitted empty:
page.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<f:view>
<h:head/>
<h:body>
<h:form>
<p:inputText id="input"
required="true"
requiredMessage="This field is required."/>
<p:message for="input"/>
<p:commandButton process="input"
update="@form"/>
</h:form>
</h:body>
</f:view>
</html>
As you can see in mojarra repo file : jsf-api/src/main/java/javax/faces/Messages.properties, line 80, the validation message you get is defined by this property:
javax.faces.component.UIInput.REQUIRED={0}: Validation Error: Value is required.
You can create your own messages properties file to override primefaces default keys, an put:
javax.faces.component.UIInput.REQUIRED={0}: Please enter value.
as this is the message you want.
To use this property file, configure context file of JSF, adding an application
xml node as follows:
<application>
<message-bundle>path.to.your.PropertiesMessageFile</message-bundle>
</application>
Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With