PDA

View Full Version : [CLOSED] B5 - validator returns isvalid ??



gslender
14 Jun 2008, 3:45 PM
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"


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);

}
}
}

darrellmeyer
17 Jun 2008, 8:05 AM
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.

gslender
17 Jun 2008, 12:10 PM
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 !!

gslender
17 Jun 2008, 5:05 PM
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.


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) ??


if (validator != null) {
String msg = validator.validate(this, value);
if (msg != null) {
markInvalid(msg);
return false;
}
}

gslender
17 Jun 2008, 5:20 PM
decided on this solution... not elegant but does the job



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;
}
};

youds
22 Jul 2008, 2:10 AM
The entire logic behind forms is invalid

gslender
2 Aug 2008, 1:47 PM
Another scenario where the text field logic is shown to be flawed...
http://extjs.com/forum/showthread.php?t=42985

Abisha
26 Mar 2009, 5:15 AM
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.