Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Form Item Trim

    I think it would be nice it text and text area items had some sort of "auto trim" feature that would remove or disallow leading spaces.

    I have been marking fields as "required: true" but it didn't take long for the users to figure out they can simply enter a space to get around it.

    Now I have to add a "changed" function that removes blanks to any text field that is required.

    #2
    You have obnoxious users and should beat them ;)

    That may make sense as a possibly stronger notion of "required" built into the framework, but bear in mind it's achievable with a regexp validation that checks that the value has non-whitespace chars. This validation rule could be shared among many different DataSources via the SimpleType system.

    Comment


      #3
      >> You have obnoxious users and should beat them ;)

      If only...

      Comment


        #4
        Customers are posting values in search fields and leading or starting white spaces are usual.
        We need to have a trim function.

        How would you achieve this with SmartGWT?


        Thanks for reply.

        Comment


          #5
          Via the regexp validator previously mentioned in this thread. Note also the ability to provide a transformTo target pattern.

          Comment


            #6
            Thanks for reply


            I don't need to validate, I need to trim it.

            What is this transformTo target pattern about? How to use it?

            Comment


              #7
              Actually it's the mask validator not the regexp validator that includes transformTo - essentially it'll perform a validation (ensure entered data matches a regular expression) and allow you to perform a transformation of the data value.
              There's an example usage here: http://www.smartclient.com/smartgwt/...alue_transform

              In your case, simply have your transform expression set to trim off leading and trailing whitespace. Validation runs automatically on saveData() so this will occur when the user submits the form, or you can call 'validate()' or 'getValidatedValues()' explicitly.

              Comment


                #8
                I was just looking for a similar thing, the desire to trim just before validating, and I found this thread. This is the solution I used, and it works with trimming alone, or in combination with validation:

                Code:
                app.validators = {
                    // other validators...
                    zip: {
                        type:"regexp", 
                        errorMessage:"Invalid ZIP code", 
                        expression:/^\d{5}(-\d{4}){0,1}$/
                    },
                    trim: {
                        type:"custom",
                        condition:function (item, validator, value) {
                            // trim function defined elsewhere
                            var trimmed = app.trim(value);
                            validator.resultingValue = trimmed;
                            item.setValue(trimmed);
                            return true;
                        }
                    }
                };
                
                // the form item
                {
                   name: "zip",
                   title: "ZIP Code",
                   validators:[app.validators.trim, app.validators.zip]
                }
                Trim found here: http://blog.stevenlevithan.com/archi...rim-javascript

                Regards,
                Bill
                Last edited by billw; 19 Sep 2011, 04:25.

                Comment


                  #9
                  Originally posted by Isomorphic View Post
                  In your case, simply have your transform expression set to trim off leading and trailing whitespace.
                  I'm trying to trim off leading and trailing whitespace off a FormItem and setting up this transform expression doesn't seem so simple.

                  Code:
                  MaskValidator maskValidator = new MaskValidator();
                  maskValidator.setMask("^\\s*.+\\s*$");   // matches zero or more whitespace characters, then one or more characters, then zero or more whitespace characters
                  maskValidator.setTransformTo("$1");
                  setValidators(maskValidator);
                  Just one example, testing the setTransformTo() method. It seems like whatever string I put in the setTransformTo() method, that's what it gets literally translated to.

                  I'm trying to follow the showcase/Javadoc example with the phone number transform, and I don't understand the "$1($2) $3 - $4" syntax and how that magically seems to work.

                  I see in the Javadoc it says "This should be set to a string in the standard format for string replacement via the native JavaScript replace() method." So I looked up the native JavaScript replace() method and the syntax there seems to be completely different than the phone number example.

                  What type of syntax do I put in the setTransformTo() method? I tried putting Regex matchers like "\\S" or ".*" to remove the whitespace, I tried putting the dollar sign arguments like in the phone number example, and no matter what I put, it just literally gets transformed to that string that I passed into setTransformTo().

                  SmartClient Version: v11.0p_2016-08-09/Enterprise Deployment (built 2016-08-09)

                  Comment


                    #10
                    It looks like you're unfamiliar with standard regex syntax: pairs of parens in the "mask" expression capture parts of the matched text that you can then use in the "transformTo" expression via the "$1" "$2" etc syntax. Each pair of parents establishes a corresponding "$" variable. So you want parens around the ".+" part of your expression.

                    Comment


                      #11
                      Correct, I actually just figured this out about 20 minutes ago via another post on this forum. I had never seen this grouping and back reference syntax before in Regex notation. Thanks for the explanation.

                      Comment


                        #12
                        Hello, as transformTo is a client only transformation, I wonder if it's possible to modify the value in a server custom DMI validator (ie to make transformations as WordUtils.capitalizeFully()).
                        I think it would be powerful to have them applied by means of a SimpleType.

                        Comment


                          #13
                          FYI, ValidationContext was added to the list of optional server custom validator parameters, you may now use validationContext.setResultingValue() API to replace the original value. See javadoc for details. NOTE this will be available in nightly builds since Oct 28 (today).

                          Comment


                            #14
                            Thank you very much, now I can define something like:

                            Code:
                            <SimpleType name="capitalizeTrimText" inheritsFrom="text" editorType="TextItem">
                                <validators>
                                    <validator type="serverCustom">
                                        <serverObject lookupStyle="new" className="myTypes.CapitalizeTrimDMI"/>
                                    </validator>
                                </validators>
                            </SimpleType>
                            Code:
                            public class CapitalizeTrimDMI {
                            
                                public boolean condition(Object value, Validator validator, String fieldName, Map record, ValidationContext validationContext) {
                                    if (value != null) {
                                        validationContext.setResultingValue(WordUtils.capitalizeFully(StringUtils.normalizeSpace(value.toString())));
                                    }
                                    return true;
                                }
                            }

                            Comment

                            Working...
                            X