1. #1
    Ext User
    Join Date
    Oct 2008
    Posts
    19
    Vote Rating
    0
    bboyle18 is on a distinguished road

      0  

    Default GXT Form Validation

    GXT Form Validation


    Hi,

    Just wondering if there are any plans to build in form validation to GXT?

    Thanks

    Brian

  2. #2
    Ext GWT Premium Member gslender's Avatar
    Join Date
    Mar 2008
    Location
    Brisbane, Australia
    Posts
    1,572
    Vote Rating
    4
    gslender is on a distinguished road

      0  

    Default


    Brian,

    FormPanel does have an isValid method that checks validation of all contained fields - with that you can have standard and custom validation for all fields.

    cheers,
    grant

  3. #3
    Ext User
    Join Date
    Oct 2008
    Posts
    19
    Vote Rating
    0
    bboyle18 is on a distinguished road

      0  

    Default


    Great. Thanks for your reply

  4. #4
    Ext User
    Join Date
    Jul 2009
    Posts
    7
    Vote Rating
    0
    retha_pasalli is on a distinguished road

      0  

    Default


    Hi..

    If I want to make an email validation in a textfield, how can I do that?
    In GWT-Ext we can use :

    TextField email = new TextField("Email", "email");
    email.setVtype(VType.EMAIL);

    How to do that in GXT? Is there any tutorial?
    Thx for any replies..

    Regards,
    Retha

  5. #5
    Sencha User
    Join Date
    Aug 2008
    Posts
    226
    Vote Rating
    2
    TheBuzzer is on a distinguished road

      1  

    Default


    Code:
    TextField<String> emailaddress = new TextField();
    emailaddress.setValidator(validator);
    
        private Validator validator = new Validator() {
    
            @Override
            public String validate(Field<?> field, String value) {
                if (field == emailaddress) {
                    if (!emailaddress.getValue().toLowerCase().matches("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])")) {
                        return "Bad E-mail Address";
                    }
                }
    
                return null;
            }
    
        };

  6. #6
    Ext User
    Join Date
    Jul 2009
    Posts
    7
    Vote Rating
    0
    retha_pasalli is on a distinguished road

      0  

    Default


    Thx TheBuzzer...

    I also find the answer :

    TextField<String> email = new TextField();
    email.setRegex(".+@.+\\.[a-z]+");
    email.getMessages().setRegexText("Bad email address!!");
    email.setAutoValidate(true);

  7. #7
    Ext User
    Join Date
    Jul 2009
    Posts
    1
    Vote Rating
    0
    toff63 is on a distinguished road

      0  

    Default


    Hey,

    You also can set your validators using regular expression from the one used ext-3.0.0\src\widgets\form\VTypes.js.
    I created an enum Class:
    Code:
    package com.example.form.client;
    
    public enum VType {
      ALPHABET("^[a-zA-Z_]+$", "Alphabet"), 
      ALPHANUMERIC("^[a-zA-Z0-9_]+$", "Alphanumeric"), 
      NUMERIC("^[+0-9]+$", "Numeric"),
      EMAIL("^(\\w+)([-+.][\\w]+)*@(\\w[-\\w]*\\.){1,5}([A-Za-z]){2,4}$", "Email");
      String regex;
      String name;
    
      VType(String regex, String name) {
        this.regex = regex;
        this.name = name;
      }
    }
    and a class implementing Validator interface:
    Code:
    public class VTypeValidator implements Validator {
    
      private VType type;
      
      public VTypeValidator(VType type){
        this.type = type;
      }
      @Override
      public String validate(Field<?> field, String value) {
        String res = null;
        if(!value.matches(type.regex)){
          res = value + "isn't a valid " + type.name;
        }
        return res;
      }
    
    }
    With those two classes you hanlde field checks like this:
    Code:
    extField.setValidator(new VTypeValidator(VType.EMAIL));
    I still don't understand why it isn't as simple as with Ext core to handle fields checks.

    Hope it helps.

    Christophe

  8. #8
    Ext User
    Join Date
    Apr 2009
    Posts
    6
    Vote Rating
    0
    Hova is on a distinguished road

      0  

    Default


    good post chris

  9. #9
    Sencha Premium Member Neilcoder's Avatar
    Join Date
    Apr 2011
    Location
    Ireland
    Posts
    264
    Vote Rating
    0
    Neilcoder is on a distinguished road

      1  

    Default


    I was trying to figure out how to validate emails in GXT3 so I updated this but it seems you don't add validators like you could in 2008.

    I assume there's a simple way to validate email somehow? I will keep looking.

    PHP Code:
    import java.util.ArrayList;
    import java.util.List;

    import com.google.gwt.editor.client.Editor;
    import com.google.gwt.editor.client.EditorError;
    import com.sencha.gxt.widget.core.client.form.Validator;
    import com.sencha.gxt.widget.core.client.form.error.DefaultEditorError;

    public class 
    VTypeValidator<T> implements Validator<T> {

          private 
    VType type;
          
          public 
    VTypeValidator(VType type){
            
    this.type type;
          }

        @
    Override
        
    public List<EditorErrorvalidate(Editor<TeditorT value) {
            List<
    EditorErrorres null;
            if (
    value == null || "".equals(value)) {
                List<
    EditorErrorerrors = new ArrayList<EditorError>();
                
    errors.add(new DefaultEditorError(editor"Fail"""));
                return 
    errors;
              }
            return 
    res;
        }
          


    Update


    I think its like this; (GXT 3)

    PHP Code:
    RegExValidator myValidator = new RegExValidator("^(\\w+)([-+.][\\w]+)*@(\\w[-\\w]*\\.){1,5}([A-Za-z]){2,4}$""Email"); 
    not too sure how to add it yet...

    just trying to figure out how to add this

    .....

    update

    got it!
    PHP Code:
        RegExValidator myEmailValidator = new RegExValidator("^(\\w+)([-+.][\\w]+)*@(\\w[-\\w]*\\.){1,5}([A-Za-z]){2,4}$""Email");
        
    email.getValidators().add(myEmailValidator); 

  10. #10
    Sencha - GXT Dev Team
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,732
    Vote Rating
    90
    Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light

      1  

    Default


    Or easier still:

    Code:
    email.addValidator(new RegExValidator(...));
    Edit: And if you are using the GWT Editor Framework and get a JSR 303 ConstraintViolation from the server, you can pass that to the editor driver and it will be displayed on the TextField automatically.