0 Replies Latest reply on Feb 9, 2009 4:34 PM by Alexis Coles

    Changing values in a <h:inputText> from the bean.

    Alexis Coles Newbie
      Hi guys,

      Please bear with me as I am not only new to seam but Java as well.

      I am working on a page with a post code on it, I am using the following code to validate this field.

      <h:inputText id="postCode" value="#{keeper.postCode}"  tabindex="7" required="true" validator="#{keeperDetails.validatePostCode}">

      my validatePostCode function looks like this,

      public void validatePostCode(FacesContext context, UIComponent toValidate,
                     Object value) {
                
                String strValue = (String)value;
                strValue = strValue.toUpperCase();
                strValue = removeWhitespaces(strValue);
                Pattern p = Pattern.compile("(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|" +
                          "[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2})");
                Matcher m = p.matcher(strValue);
                boolean matchFound = m.matches();
                if(!matchFound){
                     ((UIInput) toValidate).setValid(false);
                     context.addMessage(toValidate.getClientId(context),
                               new FacesMessage(Messages.instance().get(
                                         "must be a valid postcode")));
                }
                else{
                     keeper.setPostCode(strValue);
                }
           }
           
           
           private String removeWhitespaces(String postCode) {
           /*     postCode = postCode.replace(" ", "");
                int length = postCode.length();
                String postCode2 = postCode.substring(length - 3, length);
                */
                
                StringTokenizer st = new StringTokenizer(postCode, " ");
                StringBuilder sb = new StringBuilder();
                while (st.hasMoreTokens()) {
                     sb.append(st.nextToken() + " ");               
                }
                postCode = sb.toString().trim();;
                int loc = postCode.indexOf(" ");
                if (loc == - 1){
                     int length = postCode.length();
                     String postCode2 = postCode.substring(length - 3, length);
                     String postCode1 = postCode.substring(0, length - 3);
                     postCode = postCode1 + " " + postCode2;
                }
                return postCode;
           }

      I am basicly doing a load of string manipulation before passing this through the validator and my idea was to call the set function on the object that the feild is mapped to to then set the value with this new manipulated value.

      However it does not seem to work it's like after the validation is done the value in keeper.postCode is then set again to what ever was entered into the field originally.

      Could anyone tell me how I could change the value that the user has entered with the value that I want to enter?

      Please let me know if you need some more information and I shall try to elaborate.

      Many thanks

      Alexis