Hybrid View

  1. #1
    Sencha User
    Join Date
    Dec 2010
    Location
    Bangalore, India
    Posts
    10
    Vote Rating
    0
    dineshv87 is on a distinguished road

      0  

    Lightbulb Text field validate method not calling validator

    Text field validate method not calling validator


    Hi
    I have a formpanel which has only one text field. I have added few validations via setValidator() method. I am calling textfield.validate() method on click of submit button. But validator.validate method is being called only if we do some edit in the textfield. On page load if we directly click submit button validator.validate() method is not being called. So validation is success always even if we dont have proper value. Below is the test case

    Code:
    public void onModuleLoad() {
    FormPanel formPanel = new FormPanel();
            formPanel.setHeading("Test form");
            final TextField<String> field = new TextField<String>();
            field.setFieldLabel("Test");
            field.setTitle("Test");
            field.setValidateOnBlur(true);
            //field.setAllowBlank(false);  //I dont want to set this upfront
            field.setMinLength(1);
            field.setValidator(new Validator() {
                @Override
                public String validate(Field<?> field, String value) {
                    boolean allowBlankValue = false;//this will be set based on some condition at runtime
                    if (!allowBlankValue && (value == null || value.trim().length() < 1)) {
                        return "Enter valid value";
                    }
                    if (!value.trim().equalsIgnoreCase("test")) {
                        return "Text not matching";
                    }
                    return null;
                }
            });
            formPanel.add(field);
            Button submitButton = new Button("Submit");
            submitButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
                @Override
                public void componentSelected(ButtonEvent ce) {
                    if (field.validate()) {
                        Window.alert("success");
                    }
                }
            });
            formPanel.addButton(submitButton);
    
            RootPanel.get().add(formPanel);
    }
    Am I missing anything here or is this the issue? Thanks in advance.

    GXT Version : 2.2.3
    GWT Version : 2.2.0

  2. #2
    Sencha User
    Join Date
    Sep 2007
    Posts
    13,971
    Vote Rating
    132
    sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light

      0  

    Default


    Take a look at the validateValue method of TextField. There you will see why it does not get called when the field is blank and you allow it to be blank.

  3. #3
    Sencha User
    Join Date
    Dec 2010
    Location
    Bangalore, India
    Posts
    10
    Vote Rating
    0
    dineshv87 is on a distinguished road

      0  

    Default


    Thanks for the response.

    Yes I understood why it is not calling validator.validate() method. But as per the document of textfield.setValidator(...) method it should be called when all basic validations are success (Irrespective of whether you have value in the textfield or not). Take the below scenario.

    I have one CheckBox and one TextField. My TextField validation depends on the CheckBox value. If CheckBox is checked TextField should not allow blank values. If CheckBox is unchecked TextField is optional. In this scenario I am expecting validator method to be executed even there is no value entered in the TextField. I can solve this issue by adding Listner to the CheckBox and call the setAllowBlank(...) method. Still I don't like the way adding event listners for simple validation. After that code will become messy with lots of listeners. There will be some cases in future I will be dealing with cancelling events with CheckBox.

    Is it possible to make GXT to call a validator.validate() method even if there is no value entered other than asking me to override the TextField.validateValue() method? Please correct me if I have understood wrongly. :-)

  4. #4
    Sencha User
    Join Date
    Sep 2007
    Posts
    13,971
    Vote Rating
    132
    sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light

      0  

    Default


    This cannot be changed as it is a huge braking change. For now you need to extend Textfield to add your custom logic.

  5. #5
    Sencha User
    Join Date
    Aug 2009
    Posts
    96
    Vote Rating
    1
    r4nd7263 is on a distinguished road

      0  

    Default


    Would be nice if this is fixed in the future, or at least update doc to make it clear. I wasted quite some time to realize this behavior. Kept thinking there is a problem with my code.

  6. #6
    Sencha User abshnasko's Avatar
    Join Date
    Sep 2011
    Posts
    44
    Vote Rating
    0
    abshnasko is on a distinguished road

      0  

    Default


    Agreed -- documentation does not mention this caveat at all. An update for 2.2.6 or 3.0, perhaps?

Thread Participants: 3

Tags for this Thread