PDA

View Full Version : form validation



fother
20 Jan 2009, 2:21 AM
final FormPanel form = new FormPanel();

final TextField<String> field = new TextField<String>();
field.setAllowBlank(false);
field.setRegex("[a-zA-Z]{3}");
field.getMessages().setRegexText("regex");
field.setValidator(new Validator<String, TextField<String>>() {

public String validate(TextField<String> field, String value) {

if (value.equals("aaa")) {
field.forceInvalid("aaa");
} else {
field.clearInvalid();
}

return null;
}

});

Button valid = new Button("mark valid");
valid.addSelectionListener(new SelectionListener<ComponentEvent>() {

@Override
public void componentSelected(ComponentEvent ce) {
field.clearInvalid();
}
});

Button invalid = new Button("mark invalid");
invalid.addSelectionListener(new SelectionListener<ComponentEvent>() {

@Override
public void componentSelected(ComponentEvent ce) {
field.forceInvalid("invalid");
}
});

Button test = new Button("test");
test.addSelectionListener(new SelectionListener<ComponentEvent>() {

@Override
public void componentSelected(ComponentEvent ce) {

if (form.isValid()) {
System.out.println("valid");
} else {
System.out.println("invalid");
}

}
});

form.add(field);
form.add(valid);
form.add(invalid);
form.add(test);

RootPanel.get().add(form);


typed "aaa" in the field, and click on the button "test" - return valid, if you click again return invalid.

in this method I need use "forceInvalid" and "clearInvalid", because I use a RPC that return if what the user typed isn't registered in data base.


public String validate(TextField<String> field, String value) {

if (value.equals("aaa")) {
field.forceInvalid("aaa");
} else {
field.clearInvalid();
}

return null;
}


any ideai?

fother
20 Jan 2009, 2:25 AM
other question..

typed "aaa" click on the button "test", return valid, click again, return invalid... now click on the button mark valid, then click on the button "test", return valid, click again return invalid...

:-/

fother
20 Jan 2009, 2:37 AM
to solve this problem I need do this..



Button test = new Button("test");
test.addSelectionListener(new SelectionListener<ComponentEvent>() {

@Override
public void componentSelected(ComponentEvent ce) {

field.isValid();

if (form.isValid()) {
System.out.println("valid");
} else {
System.out.println("invalid");
}

}
});


bug?

sven
20 Jan 2009, 4:49 AM
Yes, one within your code:



public String validate(TextField<String> field, String value) {

if (value.equals("aaa")) {
field.forceInvalid("aaa");
} else {
field.clearInvalid();
}

return null;
}


http://www.extjs.com/deploy/gxtdocs/com/extjs/gxt/ui/client/widget/form/Validator.html#validate(F,%20java.lang.String)

Still not reading the docs or?

fother
20 Jan 2009, 5:45 AM
I was trying to simulate a problem .. I think no more successful .. rsss .. Next .. when we used to validate rpc .. takes a while to return a value and mark as invalid .. For example: to determine whether or not a record was saved in the database ..

to solve this.. I needed validate on server, and return error