Hybrid View

  1. #1
    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 [CLOSED] B5 - validator returns isvalid ??

    [CLOSED] B5 - validator returns isvalid ??


    Howdy...

    When calling isValid against a FormPanel, any fields with a custom validator are not checked correctly.

    The snippet below shows problem...

    1) Enter values for all but last field (password2 - which has the custom validator)

    2) click check and it returns True suggesting all fields have been checked for isvalid - clearly password2 did not have its isvalid correctly called??

    Looking at the GXT source I think the overridden validateValue(..) method needs an extra condition to consider if the validator is used (as it may be ok for the value length to be < 1)

    Perhaps consider moving the validator check to the top as if one is set, the other validators are kinda not needed (true?) - maybe add docs to say "if you set a validator, it will be called before all other set validations etc"

    Code:
    package com.mycompany.gxt.client;
    
    import com.google.gwt.core.client.EntryPoint;
    import com.google.gwt.user.client.ui.RootPanel;
    
    import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
    import com.extjs.gxt.ui.client.event.ComponentEvent;
    import com.extjs.gxt.ui.client.event.SelectionListener;
    import com.extjs.gxt.ui.client.widget.ContentPanel;
    import com.extjs.gxt.ui.client.widget.Info;
    import com.extjs.gxt.ui.client.widget.LayoutContainer;
    import com.extjs.gxt.ui.client.widget.Viewport;
    import com.extjs.gxt.ui.client.widget.button.Button;
    import com.extjs.gxt.ui.client.widget.form.FormPanel;
    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.form.FormPanel.LabelAlign;
    import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
    
    public class ValidatorTest implements EntryPoint {
    
        public void onModuleLoad() {
    
            ContentPanel cp = new ContentPanel();
            cp.setHeading("Test");
            cp.setSize(600, 400);
    
            LoginContainer lc = new LoginContainer();
            cp.add(lc);
    
            final Viewport vp = new Viewport();
            vp.add(cp);
            vp.layout();
    
            RootPanel.get().add(vp);
        }
    
        public class LoginContainer extends LayoutContainer {
            public LoginContainer() {
                super();
                setLayout(new CenterLayout());
    
                final FormPanel panel = new FormPanel();
                panel.setHeading("Validator test");
                panel.setFrame(true);
                panel.setFieldWidth(100);
                panel.setLabelWidth(80);
                panel.setLabelAlign(LabelAlign.RIGHT);
                panel.setButtonAlign(HorizontalAlignment.RIGHT);
                panel.setWidth(220);
    
                final TextField<String> userTxtFld = new TextField<String>();
                userTxtFld.setFieldLabel("Username");
                userTxtFld.setEmptyText("Username");
                userTxtFld.setAllowBlank(false);
                userTxtFld.setSelectOnFocus(true);
                panel.add(userTxtFld);
    
                final TextField<String> pwd1TxtFld = new TextField<String>();
                pwd1TxtFld.setPassword(true);
                pwd1TxtFld.setAllowBlank(false);
                pwd1TxtFld.setFieldLabel("Password1");
                panel.add(pwd1TxtFld);
    
                final TextField<String> pwd2TxtFld = new TextField<String>();
                pwd2TxtFld.setPassword(true);
                pwd2TxtFld.setFieldLabel("Password2");
                pwd2TxtFld.setValidator(new Validator<String, TextField<String>>() {
                    public String validate(TextField<String> field, String value) {
                        if (!value.equals(pwd1TxtFld.getValue())) return "Passwords do not match";
                        return null;
                    }
                });
                panel.add(pwd2TxtFld);
    
                final Button isvalidBtn = new Button("Check");
                panel.addButton(isvalidBtn);
    
                SelectionListener<ComponentEvent> listener = new SelectionListener<ComponentEvent>() {
                    public void componentSelected(ComponentEvent be) {
                        if (panel.isValid()) Info.display("isValid", "is true??");
                    }
                };
                isvalidBtn.addSelectionListener(listener);
    
                add(panel);
    
            }
        }
    }
    Last edited by gslender; 17 Jun 2008 at 5:20 PM. Reason: figured out a way to get a solution...

  2. #2
    Sencha - GXT Dev Team darrellmeyer's Avatar
    Join Date
    May 2007
    Location
    Washington, DC
    Posts
    2,242
    Vote Rating
    2
    darrellmeyer is on a distinguished road

      0  

    Default


    TextField defaults with allowBlank to true. So when the validation is run and the field is empty, it passes validation and the validate method will not be called. If you do not want to allow blank values then call setAllowBlank(false) just as you did with password 1 field.

  3. #3
    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


    no, this is not right - cases where you want to custom validate (regardless of allow blank etc) should be allowed (one example is where you want to compare passwords or and blank is acceptable, but only if another field is also blank).

    Please reconsider this as I think the logic is wrong !!

  4. #4
    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


    Possible solution is to override validateValue and change the behaviour, but unfortunately validator is marked private so it cannot be accessed as per this code...

    I have a requirement to check validation via custom (but also allow blank) unfortunately current code can't allow this condition.

    Code:
    final TextField<String> pwd2TxtFld = new TextField<String>() {
    				@Override
    				protected boolean validateValue(String value) {
    			    if (validator != null) {
    			      String msg = validator.validate(this, value);
    			      if (msg != null) {
    			        markInvalid(msg);
    			        return false;
    			      }
    			    }
    				}
    			};
    I do not see why we can't move the validator section to before the field length check at the begining of validateValue - this should not cause any ill effect.

    ie move this section to the top of validateValue before anything else... (ie only when validators are used are they checked first before all other inbuilt validation checks) ??
    Code:
        if (validator != null) {
          String msg = validator.validate(this, value);
          if (msg != null) {
            markInvalid(msg);
            return false;
          }
        }

  5. #5
    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


    decided on this solution... not elegant but does the job

    Code:
    			final TextField<String> pwd2TxtFld = new TextField<String>() {
    				@Override
    				protected boolean validateValue(String value) {				
    					if (!value.equals(pwd1TxtFld.getRawValue()))
    					{
    				        markInvalid("Passwords do not match");
    						return false;
    					}
    					return true;
    				}
    			};

  6. #6
    Sencha User youds's Avatar
    Join Date
    Jun 2008
    Posts
    8
    Vote Rating
    0
    youds is on a distinguished road

      0  

    Default


    The entire logic behind forms is invalid

  7. #7
    Ext User
    Join Date
    Mar 2009
    Posts
    87
    Vote Rating
    0
    Abisha is on a distinguished road

      0  

    Default Thanks

    Thanks


    Quote Originally Posted by darrellmeyer View Post
    TextField defaults with allowBlank to true. So when the validation is run and the field is empty, it passes validation and the validate method will not be called. If you do not want to allow blank values then call setAllowBlank(false) just as you did with password 1 field.

    Thank you for your kind information. I wasted 2hours with that.
    your message is cleared.