4 Replies Latest reply on Jun 27, 2008 12:52 AM by rogermori1

    Can I put a regular expression work from a messages.property file?

    rstevens

      I wish there was a hibernate validator for Phone. I could hardcode my regular expression into a Java file but since phone numbers change for each language, I want to put the expression into a messages.properties file. Does anyone have a working example of how to do this?

        • 1. Re: Can I put a regular expression work from a messages.property file?
          rogermori1

          Hi Robert:


          Di you have any luck?


          Thank you.


          Roger.

          • 2. Re: Can I put a regular expression work from a messages.property file?
            rstevens

            Roger,

            No we did not have any luck. We couldn't get regular expressions working from the message.properties file. We kept the regular expression code in the the model java files.

            I"m sure this community doesn't want to hear this: We put the Seam project on hold and redid our application in Ruby on Rails. We got tired of rebooting the server, random crashes during startup, error dumps with little useful information and the prospect of nobody qualified to take over. It was just too painful.

            If we're successful we may go back and finish the Seam version.

            Robert

            • 3. Re: Can I put a regular expression work from a messages.property file?
              dhinojosa

              That sucks that no one answered your question in a timely manner.


              For what it's worth, if I were to take a stab at it, I would probably use the messages variable to get the correct regex for the phone number


              @Name("phoneValidator")
              @Scope(ScopeType.CONVERSATION)
              @Validator
              public class IntlPhoneValidatorBean implements javax.faces.validator.Validator {
              
              
                  @In("messages[phoneRegex]")
                  private String phoneRegex;
              
                  public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
                    //Match regular expression             
                    //if it doesn't match, throw a ValidatorException with the correct message key  
                  }
              }
              



              I would then plug that into whatever requires it.


              I hope you come back.;)

              • 4. Re: Can I put a regular expression work from a messages.property file?
                rogermori1

                Thanks Daniel.


                Based on Daniel's feeback I coded and tested the following telephone validator reading a regular expression from the properties file.


                Because of having problem with the @In annotation, the validator gets the regular expression programmatically accessing the ResourceBoundle.



                Here is a complete example:.


                
                package com.dpn.action.common;
                
                
                import java.util.regex.Matcher;
                
                import java.util.regex.Pattern;
                
                import java.util.regex.PatternSyntaxException;
                
                
                import javax.faces.application.FacesMessage;
                
                import javax.faces.component.UIComponent;
                
                import javax.faces.context.FacesContext;
                
                import javax.faces.validator.ValidatorException;
                
                
                import org.jboss.seam.ScopeType;
                
                import org.jboss.seam.annotations.Create;
                
                import org.jboss.seam.annotations.Logger;
                
                import org.jboss.seam.annotations.Name;
                
                import org.jboss.seam.annotations.Scope;
                
                import org.jboss.seam.annotations.Startup;
                
                import org.jboss.seam.annotations.faces.Validator;
                
                import org.jboss.seam.core.SeamResourceBundle;
                
                import org.jboss.seam.log.Log;
                
                
                
                @Name("telephoneValidator")
                
                @Validator(id = "com.dpn.action.common.TelephoneValidator")
                
                @Scope(ScopeType.SESSION)
                
                @Startup
                
                public class TelephoneValidator implements javax.faces.validator.Validator {
                
                     @Logger
                
                     private Log log;
                
                     
                
                     /*
                
                      *  On the properties file:
                
                      *     telephone.regExp=(\d{3}-)?\d{3}-\d{4}( [xX] \d+)?
                
                      */
                
                     
                
                     /*
                
                      * View
                
                        <h:inputText value="#{partySummaryMgr.myPartySummary.telephoneNumber}"
                
                                         id="partySummary_mp_id_105" maxlength="32" size="32" 
                
                                         
                
                                    >
                
                          <f:validator validatorId="com.dpn.action.common.TelephoneValidator" />
                
                          <a:support event="onblur"  ajaxSingle="true"
                
                                 bypassUpdates="true"
                
                                 reRender="partySummary_mp_id_100"
                
                                 eventsQueue="changesQueue" />
                
                        </h:inputText>
                
                      */
                
                     
                
                     private String defaultRegExp = "(\\d{3}-)?\\d{3}-\\d{4}( [xX] \\d+)?";
                
                     
                
                     private String telephonephoneRegExp;
                
                     private String msgInvalid;
                
                     private String msgNotMatch;
                
                     
                
                     public TelephoneValidator(){}
                
                     
                
                     public void validate(FacesContext context, 
                
                                            UIComponent component, 
                
                                          Object value) {
                
                          
                
                          if (value == null) {
                
                               return;
                
                          }
                
                          
                
                          FacesMessage invalidMessage = new FacesMessage(msgInvalid);
                
                          invalidMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
                
                          
                
                          
                
                          FacesMessage mismatchMessage = new FacesMessage(msgNotMatch);
                
                          mismatchMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
                
                          
                
                          String _telephoneNumber = value.toString();
                
                          
                
                          if (telephonephoneRegExp == null ){
                
                               throw new ValidatorException(invalidMessage);
                
                          }
                
                         try {  
                
                                Pattern pattern = Pattern.compile(telephonephoneRegExp);
                
                                Matcher matcher = pattern.matcher(_telephoneNumber);
                
                                if (matcher.matches()) {
                
                                     return;
                
                                }
                
                          
                
                         } catch (PatternSyntaxException   ex ) {
                
                             throw new ValidatorException(invalidMessage);
                
                        } catch (IllegalArgumentException ex ) {
                
                             throw new ValidatorException(invalidMessage);
                
                        } 
                
                        throw new ValidatorException(mismatchMessage);        
                
                  }
                
                
                     @Create
                
                     public void create() {
                
                          java.util.ResourceBundle svBundle = SeamResourceBundle.getBundle();
                
                          telephonephoneRegExp = svBundle.getObject("telephone.regExp").toString();
                
                          msgInvalid           = svBundle.getObject("telephone.regExp.invalid").toString();
                
                          msgNotMatch          = svBundle.getObject("telephone.notMatch").toString();
                
                         
                
                         log.info("*************New Instance**************");
                
                          log.info("telephonephoneRegExp = " + telephonephoneRegExp);
                
                          log.info("msgInvalid           = " + msgInvalid);
                
                          log.info("msgNotMatch          = " + msgNotMatch);
                
                     }
                
                }