Announcement

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

    Form Validations

    Isomorphic,

    We have some custom components, which needs to be validated without changing much code(UI is some what complex)

    The examples shown in showcase dint helped me much and i searched in forum also. For example, we have a type ahead text box, which takes 2 letter code(multiple values can be entered by comma separated)

    we need to validate for required field aswell as valid entry. Please let me know how can we achieve this. As i mentioned, my form has so many form items which has to be validated. which is the best approach. Please point me in right direction.

    Thanks,
    Yathish

    #2
    Sorry, there's nothing we can do with a vague question like this because we don't know what limitation or problem you perceive with the documented validation approaches. You will need to ask more specific questions.

    Comment


      #3
      I was going through RegExpValidator.

      my expression is (([A-Z0-9]{3})+)(,([A-Z0-9]{3})+)*

      my input is AAAA.

      J2SE returns false for System.out.println("blr".matches("(([A-Z0-9]{3})+)(,([A-Z0-9]{3})+)*"));

      but with smartGWT, it goes for validations and hence returns true. I was expecting the red error image but it dint.

      Let me know is this is a known bug or GWT has another set of regular expressions.


      Also, I have a TextItem with some Hint. I have a regular expression which needs to be validated only when some value is entered. If nothing is entered it should not validate. How can i do that? Please let me know.

      Thanks,
      Yathish

      Comment


        #4
        It's most likely a problem with how you are quoting the expression, especially if it's in XML - we need to see the context of where you're defining it.

        Comment


          #5
          Here is my code.

          Code:
          TextItem origin = new TextItem();
          origin.setValidators("(([A-Z0-9]{3})+)(,([A-Z0-9]{3})+)*");
          Am doing form.validate() on click of a button. Let me know what is the issue. This regular expression works fine for all my inputs in simple java program but not here.

          Thanks,
          Yathish

          Comment


            #6
            No such API - if you want help please show actual code, not fictitious code that doesn't compile. If we're looking at fake code, we can't spot real issues.

            Comment


              #7
              Sorry. Here is the code.

              Code:
              TextItem origin = new TextItem();
              RegExpValidator validator = new RegExpValidator();
              validator.setExpression("(([A-Z0-9]{3})+)(,([A-Z0-9]{3})+)*");
              origin.setValidators(validator );
              my input is AAAA.

              J2SE returns false for System.out.println("blr".matches("(([A-Z0-9]{3})+)(,([A-Z0-9]{3})+)*"));

              but with smartGWT, it goes for validations and hence returns true. I was expecting the red error image but it dint.

              Comment


                #8
                We're not reproducing a problem with allowing "blr".

                Your regexp will match "AAAA" because it contains AAA (matches first parenthesized expression) and your regexp is not anchored so other text is allowed afterwords. To anchor the expression add "^" at the beginning and "$" at the end (standard regex).

                Comment


                  #9
                  But with plain java it returns false with the same expression.

                  Here is my inputs.

                  BLR - valid
                  BLR, - valid
                  BLR,BOM -valid
                  BLR,BO - invalid
                  BLR,BOM, - valid
                  BLRB - invalid

                  I will get this with the regex which i have but not through smartgwt. Any issues with my code/regex?

                  Comment


                    #10
                    Java's String.matches() assumes anchors, regexp validators do not.

                    Comment


                      #11
                      So adding "^" at the beginning and "$" at the end would resolve my issue. Let me try and let you know.

                      Thanks for your help.

                      Comment


                        #12
                        This worked fine. Thanks.

                        But i have a another issue. I have a select item and a text item. the default validation works fine but once i change select item required validation wont work.

                        Here is my code.

                        Code:
                        TextItem origin = new TextItem();
                        origin.setRequired(true);
                        RegExpValidator validator = new RegExpValidator();
                        validator.setExpression("^(([A-Z0-9]{3})+)(,([A-Z0-9]{3})+)*(,)*$"); //which validates airports but not the country.
                        origin.setValidators(validator);
                        
                        SelectItem selectType = new SelectItem();
                        selectType .setValueMap("Airport", "Country");
                        selectType.addChangedHandler(new ChangedHandler() {
                                    public void onChanged(ChangedEvent changedEvent) {
                                         origin.setRequired(true);
                                         getField("origin").clearValue();
                                         RegExpValidator validator = new RegExpValidator();
                                         if((String) changedEvent.getValue().equalsignorecase("Airport)
                                            validator.setExpression("^(([A-Z0-9]{3})+)(,([A-Z0-9]{3})+)*(,)*$"); 
                                        else 
                                            validator.setExpression("^(([A-Z0-9]{2})+)(,([A-Z0-9]{2})+)*(,)*$"); 
                        }
                        Note that, regardless of the selection of Airport/country, it should validate a textItem for required field. If it is Default(Airport selected) error message displays. But if i change it to Country Required field validation fails.

                        Please let me know what is the issue/is it a known issue?

                        Comment


                          #13
                          This code isn't making much sense - a validator is created on the fly but not attached to anything? And it shows setting a field to required that is already required.

                          Please examine these problems - if you need more help, please re-explain the issue then show code that we can run to see the problem.

                          Comment


                            #14
                            I have attached this validator at runtime to the TextItem, which i missed while typing.

                            But even though, i have set required field, it wont throws error. Here is my final code.

                            Code:
                            final TextItem origin = new TextItem();
                            origin.setRequired(true);
                            RegExpValidator validator = new RegExpValidator();
                            validator.setExpression("^(([A-Z0-9]{3})+)(,([A-Z0-9]{3})+)*(,)*$"); //which validates airports but not the country.
                            origin.setValidators(validator);
                            
                            final SelectItem selectType = new SelectItem();
                            selectType .setValueMap("Airport", "Country");
                            selectType.addChangedHandler(new ChangedHandler() {
                            	public void onChanged(ChangedEvent changedEvent) {
                            		getField("origin").clearValue();
                            		RegExpValidator validator = new RegExpValidator();
                            		if((String) changedEvent.getValue().equalsignorecase("Airport)
                            			validator.setExpression("^(([A-Z0-9]{3})+)(,([A-Z0-9]{3})+)*(,)*$"); 
                            		else 
                            			validator.setExpression("^(([A-Z0-9]{2})+)(,([A-Z0-9]{2})+)*(,)*$");
                            		origin.setValidators(validator);
                            	}
                            }

                            Comment


                              #15
                              We don't recommend changing validators on the fly, it has a lot of ambiguity with respect to what is supposed to happen to existing errors and values that may have just become invalid as of new validators being applied. If your validator is conditional, add it as a custom validator.

                              Comment

                              Working...
                              X