More JSF Validation - Custom Error Messages

The errors messages that are shown for each type of validation error are controlled by the Message.properties file, which is located in the javax.faces package of jsf-impl.jar. You can view this file by expanding Libraries > Sun Java System Application Server > jsf-impl.jar > javax.faces and double-clicking Message.properties.

If you want to create custom messages for these errors, you can do so by swapping out the properties file used by the application.

  1. Right-click the jAstrologer project and choose New > File/Folder. Under the Other category, select Properties File and click Next. Name the file MyMessages, put it in the src/astrologer/ui folder, and click Finish. MyProperties.properties opens in the Source Editor.
  2. Copy over the following properties from Message.properties to MyProperties.properties:
    javax.faces.component.UIInput.REQUIRED={0}: Validation Error: Value is required.
    javax.faces.converter.DateTimeConverter.DATE_detail={2}: ''{0}'' could not be understood as a date. Example: {1}
  3. Change the values of the properties:
    javax.faces.component.UIInput.REQUIRED=Please enter a value for this field.
    	javax.faces.converter.DateTimeConverter.DATE=Please enter a valid date. 
    javax.faces.converter.DateTimeConverter.DATE_detail=Please enter a valid date. Example: {1}
  4. Open faces-config.xml (under the Configuration Files node) and enter the following inside the main faces-config element:
        ...
        <application>
            <locale-config>
                <default-locale>en</default-locale>
            </locale-config>
            <message-bundle>astrologer.ui.MyMessages</message-bundle>
        </application>
    </faces-config>
  5. Right-click the application and choose Run Project. When you do not enter anything for a required field or enter a bad date format for the birthday field, the application now shows the following errors:

    custom errors

    Note that any messages that you haven't specified in your custom properties file will be taken from the default Message.properties in jsf-impl.jar. Also, you can define a CSS style for error messages and then specify that style in the message tag by doing the following:

    <h:message for="birthday" style="errorMessage" /></p>
Comments:

Post a Comment:
Comments are closed for this entry.
About

johnc

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today