[jboss-user] [JBoss Seam] - Re: Customize validation messages

norman.richards@jboss.com do-not-reply at jboss.com
Thu Jan 4 10:54:56 EST 2007


I think this is a real pain point for people.  I through together a quick proof of concept.  It works with MyFaces, but it shouldn't take that much to make a proper component out of.  If anyone is ambitious enough, have ago at it:

package org.jboss.seam.ui;
  | 
  | import javax.faces.component.*;
  | import javax.faces.context.FacesContext;
  | import javax.faces.application.FacesMessage;
  | 
  | import org.jboss.seam.core.*;
  | 
  | public class UIRequiredInput
  |     extends UIInput
  | {
  |     String requiredMessage;
  | 
  |     public void setRequiredMessage(String requiredMessage) {
  |         this.requiredMessage = requiredMessage;
  |     }
  | 
  |     public String getRequiredMessage() {
  |         return requiredMessage;
  |     }
  | 
  |     @Override
  |     public boolean isRequired()
  |     {
  |         return true;
  |     }
  | 
  |     @Override
  |     protected void validateValue(FacesContext context, Object convertedValue)
  |     {        
  |         System.out.println("*CHECKING! " + convertedValue);
  |         if (empty(convertedValue)) {
  |             context.addMessage(getClientId(context),
  |                                new FacesMessage(FacesMessage.SEVERITY_ERROR, 
  |                                                 requiredMessage,
  |                                                 requiredMessage));
  |             System.out.println("*INVALID! " + convertedValue);
  |             setValid(false);
  |         } else {
  |             super.validateValue(context, convertedValue);
  |         }
  |     }
  | 
  |     protected boolean empty(Object value) {
  |         if (value == null) {
  |             return true;
  |         }
  |         
  |         if (value instanceof String) {
  |             String text = (String) value;
  |             if (text.length() == 0) {
  |                 return true;
  |             }
  |         }
  | 
  |         return false;
  |     }
  | 
  |     @Override
  |     public void restoreState(FacesContext context, Object state) {
  |         Object[] values = (Object[]) state;
  |         super.restoreState(context, values[0]);
  |         requiredMessage = (String) values[1];
  |     }
  |     
  |     @Override
  |     public Object saveState(FacesContext context) {
  |         Object[] values = new Object[2];
  |         values[0] = super.saveState(context);
  |         values[1] = requiredMessage;
  |         return values;
  |     }
  |    
  | }
  | 

Usage:


  | <s:input id="payee" value="#{newPayment.payee}"
  |              requiredMessage="You have to send a payment TO somebody" />


This shouldn't be taken as saying this will ever become a proper seam component.  I just put it in the ui package because it was easier to get started with.



View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3997951#3997951

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3997951



More information about the jboss-user mailing list