PDA

View Full Version : GXT Form Validation



bboyle18
10 Oct 2008, 2:58 PM
Hi,

Just wondering if there are any plans to build in form validation to GXT?

Thanks

Brian

gslender
10 Oct 2008, 3:08 PM
Brian,

FormPanel does have an isValid method that checks validation of all contained fields - with that you can have standard and custom validation for all fields.

cheers,
grant

bboyle18
10 Oct 2008, 4:26 PM
Great. Thanks for your reply

retha_pasalli
15 Jul 2009, 6:00 PM
Hi..

If I want to make an email validation in a textfield, how can I do that?
In GWT-Ext we can use :

TextField email = new TextField("Email", "email");
email.setVtype(VType.EMAIL);

How to do that in GXT? Is there any tutorial?
Thx for any replies..

Regards,
Retha

TheBuzzer
16 Jul 2009, 7:15 AM
TextField<String> emailaddress = new TextField();
emailaddress.setValidator(validator);

private Validator validator = new Validator() {

@Override
public String validate(Field<?> field, String value) {
if (field == emailaddress) {
if (!emailaddress.getValue().toLowerCase().matches("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])")) {
return "Bad E-mail Address";
}
}

return null;
}

};

retha_pasalli
16 Jul 2009, 5:32 PM
Thx TheBuzzer...

I also find the answer :

TextField<String> email = new TextField();
email.setRegex(".+@.+\\.[a-z]+");
email.getMessages().setRegexText("Bad email address!!");
email.setAutoValidate(true);

toff63
17 Jul 2009, 4:07 AM
Hey,

You also can set your validators using regular expression from the one used ext-3.0.0\src\widgets\form\VTypes.js.
I created an enum Class:

package com.example.form.client;

public enum VType {
ALPHABET("^[a-zA-Z_]+$", "Alphabet"),
ALPHANUMERIC("^[a-zA-Z0-9_]+$", "Alphanumeric"),
NUMERIC("^[+0-9]+$", "Numeric"),
EMAIL("^(\\w+)([-+.][\\w]+)*@(\\w[-\\w]*\\.){1,5}([A-Za-z]){2,4}$", "Email");
String regex;
String name;

VType(String regex, String name) {
this.regex = regex;
this.name = name;
}
}and a class implementing Validator interface:

public class VTypeValidator implements Validator {

private VType type;

public VTypeValidator(VType type){
this.type = type;
}
@Override
public String validate(Field<?> field, String value) {
String res = null;
if(!value.matches(type.regex)){
res = value + "isn't a valid " + type.name;
}
return res;
}

}With those two classes you hanlde field checks like this:

extField.setValidator(new VTypeValidator(VType.EMAIL)); I still don't understand why it isn't as simple as with Ext core to handle fields checks.

Hope it helps.

Christophe

Hova
26 Feb 2010, 2:15 AM
good post chris =D>