0
votes

I want to display custom error messages in my JSF Application.For this I have defined my own Messages.properties file and declared its location in faces-config.xml.

Here is my Messages.properties file

javax.faces.component.UIInput.REQUIRED=Invalid input.
javax.faces.component.UIInput.REQUIRED_detail={0} is required.

here is my faces-config.xml file

<?xml version="1.0" encoding="UTF-8"?>

  <faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns /javaee/web-facesconfig_2_0.xsd"
version="2.0">
 <application>
   <message-bundle>com.edifixio.common.i18n.Messages</message-bundle>
       <resource-bundle>
        <base-name>com.edifixio.common.i18n.testBundle</base-name>
        <var>tb</var>
      </resource-bundle>
 </application>
</faces-config>

now I have xhtml page

<ui:composition template="/WEB-INF/templates/globalTemplates.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:ui="http://java.sun.com/jsf/facelets">

<ui:define name="title">#{tb['REG_PAGE']}</ui:define>
<ui:define name="content">
   <h:form>
       <h:panelGrid columns="2">
           <h:outputLabel value="#{tb['EYN']}"/>
           <h:inputText required="true" id="name"/>
           <h:outputText value="" />
           <h:message for="name" style="color:red"/>
           <h:outputLabel value="#{tb['EYA']}"/>
           <h:inputText required="true" id="address"/>
           <h:outputText value="" />
           <h:message for="address" style="color:red"/>
           <h:outputLabel value="#{tb['UID']}"/>
           <h:inputText id="uid" required="true"/>
           <h:outputText value="" />
           <h:message for="uid" style="color:red;"/>
           <h:outputLabel value="#{tb['PWD']}"/>
           <h:inputSecret required="true" id="Password"/>
           <h:outputText value="" />
           <h:message for="Password" style="color:red"/>
      </h:panelGrid>
      <h:panelGrid>
           <h:commandButton action="#{regController.success}" value="Submit" />
      </h:panelGrid>
   </h:form>
</ui:define>

now my problem is that when clicking on the submit button validation occurs it prints a message like following::

j_idt11:Password is required.

This message comes for empty password field.It seems all ok. However, I would like to show the message as "Password is required" instead of "j_idt11:Password is required.".

I know that there is requiredMessage attribute in jsf using which I can achieve this very easily, but I donot want to use this requiredMessage attribute,I just want to follow the above procedure in which "id" will be passed as parameter {0} in messages.properties file and print the message as this has done. So, in a word my requirement is to show the message with only id attribute not with composite id like " j_idt11:Password is required".

I know jsf generates this composite id at runtime,so, is there a way to eliminate this "j_idt11" from id portion while displaying the error message?

1

1 Answers

0
votes

Add label attribute in h:inputText or h:inputSecret:

<h:inputSecret id="password" value="#{bean.password}"
    required="true" label="#{tb['PWD']}" />