Background
Mostly, this is a generic CSS/HTML tip, but this article demonstrates how to display a red asterisk for labels in JSF for required fields.
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
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 in a normal HTML file. Alternatively, you can put this in an external CSS file and simply reference it from your 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"/> |
Thanks!!!! 🙂
You’re welcome. Glad it helped you! 🙂