[jboss-user] [JBoss Seam] - Re: Validation: @NotNull && required=true
monkeyden
do-not-reply at jboss.com
Sat May 12 10:32:31 EDT 2007
There is no way to do it globally but you can use a phase listener:
import java.util.Iterator;
|
| import javax.faces.application.FacesMessage;
| import javax.faces.component.UIComponent;
| import javax.faces.component.UIViewRoot;
| import javax.faces.context.FacesContext;
| import javax.faces.event.PhaseEvent;
| import javax.faces.event.PhaseId;
| import javax.faces.event.PhaseListener;
|
| /**
| * Example messages.properties entries:
| * javax.faces.validator.LengthValidator.MAXIMUM="[fieldLabel]" value is greater than allowable maximum of {0} characters.
| * javax.faces.validator.LengthValidator.MINIMUM="[fieldLabel]" value must contain at least {0} characters.
| */
|
| @SuppressWarnings("serial")
| public class MessageListener implements PhaseListener {
|
| public PhaseId getPhaseId() {
| return PhaseId.RENDER_RESPONSE;
| }
|
| public void beforePhase(PhaseEvent e) {
| FacesContext fc = e.getFacesContext();
| UIViewRoot root = fc.getViewRoot();
| Iterator i = fc.getClientIdsWithMessages();
| while (i.hasNext()) {
| String clientId = (String) i.next();
| if(clientId != null) {
| UIComponent c = root.findComponent(clientId);
| if (c != null) {
| String fieldRef =
| (String) c.getAttributes().get("fieldLabel");
| if (fieldRef != null) {
| Iterator j = fc.getMessages(clientId);
| while (j.hasNext()) {
| FacesMessage fm = (FacesMessage) j.next();
| String s = fm.getSummary();
| s = s.replaceFirst("\\[fieldLabel\\]", fieldRef);
| fm.setSummary(s);
| }
| }
| }
| }
| }
| }
|
| public void afterPhase(PhaseEvent e) {
| }
| }
|
Then specify the fielLabel in the f:attribute tag:
<h:inputText value="#{contactUs.email}" size="50" maxlength="75" required="true">
| <f:attribute name="fieldLabel" value="Email"/>
| </h:inputText>
Or, if you require completely different messages for each component, you could specify a resource bundle key
<h:inputText value="#{contactUs.email}" size="50" maxlength="75" required="true">
| <f:attribute name="bundleKey" value="contactus.error.email.required"/>
| </h:inputText>
Using the latter approach, you would get the resource bundle key from the component attributes map in the listener and replace the summary text altogether.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4045194#4045194
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4045194
More information about the jboss-user
mailing list