No announcement yet.
  • 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.

    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.


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

      If only...


        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.


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


            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?


              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:

              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.


                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:

                app.validators = {
                    // other validators...
                    zip: {
                        errorMessage:"Invalid ZIP code", 
                    trim: {
                        condition:function (item, validator, value) {
                            // trim function defined elsewhere
                            var trimmed = app.trim(value);
                            validator.resultingValue = trimmed;
                            return true;
                // the form item
                   name: "zip",
                   title: "ZIP Code",
                Trim found here:

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


                  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.

                  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
                  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)


                    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.


                      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.


                        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.


                          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).


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

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