12

Can I modify this default required="true" validation message to show only "Value is required"?

formId:inputId: Validation Error: Value is required.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215

2 Answers2

26

Either use the input component's requiredMessage attribute:

<x:inputXxx ... required="true" requiredMessage="Value is required" />

Or create a properties file in the classpath which contains the custom message template:

javax.faces.component.UIInput.REQUIRED = Value is required.

and is been registered as message bundle in faces-config.xml:

<application>
    <message-bundle>com.example.CustomMessages</message-bundle>
</application>

The above example assumes that the file name is CustomMessages.properties and is been placed in com.example package. You can name and place it wherever you want.

You can find an overview of all message keys in chapter 2.5.2.4 of the JSF specification, such as javax.faces.component.UIInput.REQUIRED_detail in case you're using them.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hi BalusC, i check that both http://download.oracle.com/otndocs/jcp/jsf-2.0-fr-eval-oth-JSpec/ and http://download.oracle.com/otndocs/jcp/jsf-2.0-fr-full-oth-JSpec/ are the same (EVAL vs IMPL), could there be a mistake? – Oh Chin Boon Feb 07 '12 at 03:10
  • 2
    No. Some APIs have separate docs for this. Endusers (read: us, developers) should just read the "Evaluation" spec. Implementors (like Mojarra and MyFaces do) should read the "Implementation" spec. But for JSF spec it's indeed not different. – BalusC Feb 07 '12 at 03:14
  • Hi BalusC, can i actually override Apache Common Validator's message this way too? http://myfaces.apache.org/commons12/myfaces-validators12/tagdoc.html – Oh Chin Boon Feb 12 '12 at 01:58
  • @BalusC Can I internationalize this custom messages? I put the javax.faces.component.UIInput.REQUIRED in my internationalized JSF properties but it doesn't show this message but the default message (Design: Validation Error: Value is required.) – John Alexander Betts Oct 15 '13 at 14:54
  • @BalusC I am getting well all my internationalized messsages except the overriden JSF messages. Do you know why this could happen? – John Alexander Betts Oct 15 '13 at 15:27
  • I tried this approach, but it does not really work for me. I am reading the message from a message bundle and although it retrieves the message from the bundle, it still shows the IDs on the message.This is how the message is shown: myForm:j_idt76: Überprüfungsfehler: Wert ist erforderlich. I do not want to add a requiredMessage attribute to all my inputFields. Also note, that my input field is a p:inputField . I could only solve this, once i added a label attribute in my inputField – Stephan Apr 07 '17 at 05:33
  • I add that you can also customize, if you use, the details, using `javax.faces.component.UIInput.REQUIRED_detail` – Marco Sulla Jul 22 '21 at 12:10
1

I think i got it here.

Added label:

<p:inputText id="hotelName" value="#{editHotelBackingBean.hotel.name}" required="true" label="#{labelResource.hotelName}">

Now it looks like this:

Hotel Name: Validation Error: Value is required.

Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215