1. #1
    Ext User hossaindoula's Avatar
    Join Date
    Jan 2010
    Location
    Riyadh, K.S.A
    Posts
    8
    Vote Rating
    0
    hossaindoula is on a distinguished road

      0  

    Default TextField value problem

    TextField value problem


    I have a form which contains several TextFields. Whenever I want to get value from the fields it returns null only when it is not inside any kind of EventHandler... But if I want to get the value from any kind of Action Event Handler... Then I can get the value... Just Have a Look at the example...
    HTML Code:
    private void createFormComponents() {
            ContentPanel cp = new ContentPanel();
            cp.setHeading("INFO");
            cp.setFrame(true);
            cp.setSize(650,750);
            cp.setLayout(new RowLayout(Orientation.HORIZONTAL));
     
            String phone = "";
     
            final FormPanel panel = new FormPanel();
            panel.setHeaderVisible(false);
            panel.setEncoding(FormPanel.Encoding.MULTIPART);
            panel.setMethod(Method.POST);
            FormLayout layout = new FormLayout();
     
            FieldSet fieldSetCap = new FieldSet();
            final TextField<String> telephone = new TextField<String>();
            telephone.setFieldLabel("Telephone");
            fieldSetCap.add(telephone);
            panel.add(fieldSetCap);
     
            Button crBtn = new Button("Add");
            panel.addButton(crBtn);
            FormButtonBinding binding = new FormButtonBinding(panel);
            binding.addButton(crBtn);
            panel.setButtonAlign(HorizontalAlignment.RIGHT);
            cp.add(panel);
     
            phone = telephone.getValue(); // phone return the ""
     
     
            crBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
                     phone = telephone.getValue(); // phone return the typed value
            });
     }
    What could be the problem???

  2. #2
    Ext User
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    961
    Vote Rating
    0
    Arno.Nyhm is on a distinguished road

      0  

    Default


    Quote Originally Posted by hossaindoula View Post
    What could be the problem???
    there are no problems

    Quote Originally Posted by hossaindoula View Post
    phone = telephone.getValue(); // phone return the ""
    thats ok that it is null at this place. because at this point it is not rendered and nobody has entered some text.


    Quote Originally Posted by hossaindoula View Post
    crBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
    phone = telephone.getValue(); // phone return the typed value
    });
    }
    at this point someone has entered the text and after this you read the value.


    if you make breakpoints in your code you can see when this lines are executed.
    This forum needs your help: you got hints from the community and now you have fixed your code? dont just reply with "now its fixed" or "i found the error"! please take the time to post also an detailed answer with the working code.

    GreaseMonkey Script for a GXT-only Forum: it hides ExtJs here: New Posts • Search Results • Advanced Search form • Category overview http://www.extjs.com/forum/showthrea...041#post410041

  3. #3
    Ext User hossaindoula's Avatar
    Join Date
    Jan 2010
    Location
    Riyadh, K.S.A
    Posts
    8
    Vote Rating
    0
    hossaindoula is on a distinguished road

      0  

    Default


    OK I understand what you explained... But the problem is I am trying to validate Confirm Password field... Whenever user types text that doesn't match with the password field, it should validate... So, in that case how can I retrieve the value after typing the password and going to confirm passwod field... Because there is no event except the onblur... Here is the thing I was trying to do...
    Code:
    final TextField<String> password = new TextField<String>();
    password.setPassword(true);
    password.setFieldLabel("Password");
    password.setAllowBlank(false);
    fieldSetCap.add(password);
     
    final TextField<String> passwordCnf = new TextField<String>();
    passwordCnf.setPassword(true);
    passwordCnf.setFieldLabel("Confirm Password");
    passwordCnf.setAllowBlank(false);
    fieldSetCap.add(passwordCnf);
    //String valPwd = password.toString();
    passwordCnf.setValidator(new PasswordConfirmValidate()); // How can I retrieve the password value because here "password.getValue()" will not work...:-/
    panel.add(fieldSetCap);

  4. #4
    Ext User
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    961
    Vote Rating
    0
    Arno.Nyhm is on a distinguished road

      0  

    Default


    how looks your PasswordConfirmValidate()?

    if you search the forum for setValidator you can find many examples how to write a validator...
    This forum needs your help: you got hints from the community and now you have fixed your code? dont just reply with "now its fixed" or "i found the error"! please take the time to post also an detailed answer with the working code.

    GreaseMonkey Script for a GXT-only Forum: it hides ExtJs here: New Posts • Search Results • Advanced Search form • Category overview http://www.extjs.com/forum/showthrea...041#post410041

  5. #5
    Ext User hossaindoula's Avatar
    Join Date
    Jan 2010
    Location
    Riyadh, K.S.A
    Posts
    8
    Vote Rating
    0
    hossaindoula is on a distinguished road

      0  

    Default


    I know how to write validator... But the problem is how I can get the other field value when I validate a field... Because based upon that other field value I am validating... Here is the code... Have a look...

    Code:
    public class PasswordConfirmValidate implements Validator{
        String valPwd = "";
    
        public PasswordConfirmValidate(){
              valPwd = ???? // Here I have to get the "password" field's value
        }
        public String validate(Field<?> field, String value){
            String res = null;
            if(!valPwd .equals(value)){
                res = " Password does not match " ;
            }
            return res;
        }

  6. #6
    Ext User
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    961
    Vote Rating
    0
    Arno.Nyhm is on a distinguished road

      0  

    Default


    Quote Originally Posted by Arno.Nyhm View Post
    if you search the forum for setValidator you can find many examples how to write a validator...
    here was the same question - validation of 2 Fields (but with version 1.0)
    http://www.extjs.com/forum/showthrea...377#post203377


    valPwd = ???? // Here I have to get the "password" field's value

    why you try to access the value in the constructor?

    there are 2 ways

    PHP Code:
    public class PasswordConfirmValidate implements Validator{

        public PasswordConfirmValidate(){
        }
        public String validate(Field<?fieldString value){
            
    String res null;
            
    String valPwd mySecondField.getValue();  // your mySecondField need be final
            
    if(!valPwd .equals(value)){
                
    res " Password does not match " ;
            }
            return 
    res;
        }
    but better make it more generic:

    PHP Code:
    public class PasswordConfirmValidate implements Validator{
        TextField<String> passwordCnf;

        public PasswordConfirmValidate(TextField<String> passwordCnf){
              this.passwordCnf = passwordCnf;
          
        }
        public String validate(Field<?fieldString value){
            
    String res null;
            if(!
    passwordCnf.getValue().equals(value)){
                
    res " Password does not match " ;
            }
            return 
    res;
        }
    in this case we only store a reference to the second field and get a "fresh" value for compare at the validation time.
    This forum needs your help: you got hints from the community and now you have fixed your code? dont just reply with "now its fixed" or "i found the error"! please take the time to post also an detailed answer with the working code.

    GreaseMonkey Script for a GXT-only Forum: it hides ExtJs here: New Posts • Search Results • Advanced Search form • Category overview http://www.extjs.com/forum/showthrea...041#post410041

  7. #7
    Ext User hossaindoula's Avatar
    Join Date
    Jan 2010
    Location
    Riyadh, K.S.A
    Posts
    8
    Vote Rating
    0
    hossaindoula is on a distinguished road

      0  

    Default


    Code:
    public class PasswordConfirmValidate implements Validator{
     
       public PasswordConfirmValidate(){
       }
       public String validate(Field<?> field, String value){
           String res = null;
           String valPwd = mySecondField.getValue(); // Always it is getting null
           if(!valPwd .equals(value)){
               res = " Password does not match " ;
           }
           return res;
       }



    Code:
    public class PasswordConfirmValidate implements Validator{
       TextField<String> passwordCnf;
       public PasswordConfirmValidate(TextField<String> passwordCnf){
             this.passwordCnf = passwordCnf; // Here also the value is getting null
       }
       public String validate(Field<?> field, String value){
           String res = null;
           if(!passwordCnf.getValue().equals(value)){
               res = " Password does not match " ;
           }
           return res;
       }

    Look I have done same as you have instructed, have a look at my code:



    Code:
    public class PasswordConfirmValidate implements Validator{
    String valPwd = "";
      public PasswordConfirmValidate(TextField<String> pwdFld){
            this.valPwd = pwdFld.getValue(); 
        }
        public String validate(Field<?> field, String value){
            String res = null;
            if(!valPwd.equals(value)){
                res = valPwd + " Password does not match " + value;
            }
            return res;
        }
    }
     
     
    =====================the Form ======================
            final TextField<String> password = new TextField<String>();
            password.setPassword(true);
            password.setFieldLabel("Password");
            password.setAllowBlank(false);
     
            final TextField<String> passwordCnf = new TextField<String>();
            passwordCnf.setPassword(true);
            passwordCnf.setFieldLabel("Confirm Password");
            passwordCnf.setAllowBlank(false);
     
            passwordCnf.setValidator(new PasswordConfirmValidate(password)); // the first value is "password" fields value and I am validating on the "passwordCnf" value...

  8. #8
    Ext User
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    961
    Vote Rating
    0
    Arno.Nyhm is on a distinguished road

      0  

    Default


    PHP Code:
       public PasswordConfirmValidate(TextField<StringpasswordCnf){
             
    this.passwordCnf passwordCnf// Here also the value is getting null
       

    yes... at this point the value should be null because it is in the CONSTRUCTOR... and there no user can input values at this moment because it is not rendered at this time.


    i think you look at the wrong places when you debug.


    but maybe you miss this point: you need to use setAutoValidate(true) to see the error while the user is typing. otherwise the user see the error only if he leave the TextField.


    here is my full TestCase with my second validator and it works fine for me (pls post the next time a full example which have all elements in one file to avoid other side effects from other parts you dont copy into your codesnippet):

    PS: in my example it only updates if you type in something in the second textbox. you need to add also something to trigger the validation if you enter something in the first textbox.

    PHP Code:
    package com.mycompany.myapplication.client.forumsnippets;

    import com.extjs.gxt.ui.client.Style.Orientation;
    import com.extjs.gxt.ui.client.widget.ContentPanel;
    import com.extjs.gxt.ui.client.widget.Window;
    import com.extjs.gxt.ui.client.widget.form.Field;
    import com.extjs.gxt.ui.client.widget.form.FormPanel;
    import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
    import com.extjs.gxt.ui.client.widget.form.TextField;
    import com.extjs.gxt.ui.client.widget.form.Validator;
    import com.extjs.gxt.ui.client.widget.layout.RowLayout;
    import com.google.gwt.core.client.EntryPoint;

    /**
     * http://www.extjs.com/forum/showthread.php?p=433866#post433866
     * @author anonym
     */
    public class ForumThread433866PasswordValidator implements EntryPoint {

        public void onModuleLoad() {
            Window window = new Window();
            window.setSize(600, 600);
            window.add(createFormComponents());
            window.show();
        }

        private ContentPanel createFormComponents() {
            ContentPanel cp = new ContentPanel();
            cp.setHeading("INFO");
            cp.setFrame(true);
            cp.setSize(650, 750);
            cp.setLayout(new RowLayout(Orientation.HORIZONTAL));


            final FormPanel panel = new FormPanel();
            panel.setHeaderVisible(false);
            panel.setEncoding(FormPanel.Encoding.MULTIPART);
            panel.setMethod(Method.POST);
            cp.add(panel);

            final TextField<String> password = new TextField<String>();
            password.setPassword(true);
            password.setFieldLabel("Password");
            password.setAllowBlank(false);
            panel.add(password);

            final TextField<String> passwordCnf = new TextField<String>();
            passwordCnf.setPassword(true);
            passwordCnf.setFieldLabel("Confirm Password");
            passwordCnf.setAllowBlank(false);
            passwordCnf.setAutoValidate(true);
            panel.add(passwordCnf);

            passwordCnf.setValidator(new PasswordConfirmValidate(password));

            return cp;
        }

        public class PasswordConfirmValidate implements Validator {

            TextField<String> passwordCnf;

            public PasswordConfirmValidate(TextField<String> passwordCnf) {
                this.passwordCnf = passwordCnf;

            }

            public String validate(Field<?fieldString value) {
                
    String res null;
                if (!
    passwordCnf.getValue().equals(value)) {
                    
    res " Password does not match ";
                }
                return 
    res;
            }
        }
    }
    This forum needs your help: you got hints from the community and now you have fixed your code? dont just reply with "now its fixed" or "i found the error"! please take the time to post also an detailed answer with the working code.

    GreaseMonkey Script for a GXT-only Forum: it hides ExtJs here: New Posts • Search Results • Advanced Search form • Category overview http://www.extjs.com/forum/showthrea...041#post410041

  9. #9
    Ext User
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    961
    Vote Rating
    0
    Arno.Nyhm is on a distinguished road

      0  

    Default


    Quote Originally Posted by Arno.Nyhm View Post
    PS: in my example it only updates if you type in something in the second textbox. you need to add also something to trigger the validation if you enter something in the first textbox.
    if you add this snippet, then it shows for the second textfield also the validation if you change the values in the first textfield:

    PHP Code:
            // new start
            password.setValidator(new Validator() {

                public String validate(Field<?fieldString value) {
                    if (
    passwordCnf.getValue() != null) {
                        
    passwordCnf.isValid();
                    }
                    return 
    null;

                }
            });
            
    // new end
            
    passwordCnf.setValidator(new PasswordConfirmValidate(password));
    This forum needs your help: you got hints from the community and now you have fixed your code? dont just reply with "now its fixed" or "i found the error"! please take the time to post also an detailed answer with the working code.

    GreaseMonkey Script for a GXT-only Forum: it hides ExtJs here: New Posts • Search Results • Advanced Search form • Category overview http://www.extjs.com/forum/showthrea...041#post410041

  10. #10
    Ext User hossaindoula's Avatar
    Join Date
    Jan 2010
    Location
    Riyadh, K.S.A
    Posts
    8
    Vote Rating
    0
    hossaindoula is on a distinguished road

      0  

    Default


    It worked... Actually the problem was in initialization of the value in the constructor...

Thread Participants: 1