-
15 Jun 2011 11:53 PM #1
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
Am I missing anything here or is this the issue? Thanks in advance.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); }
GXT Version : 2.2.3
GWT Version : 2.2.0
-
23 Jun 2011 3:01 AM #2
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.
-
23 Jun 2011 4:37 AM #3
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. :-)
-
23 Jun 2011 4:39 AM #4
This cannot be changed as it is a huge braking change. For now you need to extend Textfield to add your custom logic.
-
19 Sep 2011 8:54 AM #5
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.
-
29 Jan 2012 6:12 PM #6
Agreed -- documentation does not mention this caveat at all. An update for 2.2.6 or 3.0, perhaps?


Reply With Quote