Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primefaces custom validation message [duplicate]

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.

like image 632
dileep H Avatar asked Dec 18 '22 22:12

dileep H


2 Answers

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>
like image 80
Smutje Avatar answered Dec 28 '22 07:12

Smutje


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!

like image 35
malaguna Avatar answered Dec 28 '22 06:12

malaguna