Background
[wp_ad_camp_5]
For the most part, this is a generic CSS/HTML tip but this article demonstrates how to display a red asterisk for labels in JSF for required fileds.
Software Environment
- Windows 7 Professional SP1
- Eclipse – Kepler Release
- Java 1.7 (1.7.0_67 – Windows x86)
- JSF 2.2
- Downloaded, and referenced as part of a Dynamic Web Project in Eclipse
- Primefaces 4.0
- Downloaded from www.primefaces.org
- Place it in the project in WebContent\WEB-INF\lib and reference it
[wp_ad_camp_4]
The Red Asterisk
To create a red asterisk that displays before any labels in JSF for required fields, simply create an inline CSS.
1 2 3 |
<style> .required:before { content:"* ";color: red; } </style> |
You may place this in your .xhtml file just like you would in a normal html file. Alternatively, you can put this in an external CSS file and simply reference it from you page.
Using Your Inline CSS
Just below your inline CSS, use styleClass to reference the style class.
1 2 3 4 5 |
<p:outputLabel value="Name:" styleClass="required" /> <p:inputText id="name" value="#{mycontroller.name}" required="true" requiredMessage="Name is required"/> |
[wp_ad_camp_3]
Thanks!!!! 🙂
You’re welcome. Glad it helped you! 🙂