Hi,
Just wondering if there are any plans to build in form validation to GXT?
Thanks
Brian
Printable View
Hi,
Just wondering if there are any plans to build in form validation to GXT?
Thanks
Brian
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
Great. Thanks for your reply
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
Code:
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;
}
};
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);
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:
and a class implementing Validator interface:Code: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;
}
}
With those two classes you hanlde field checks like this:Code: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;
}
}
I still don't understand why it isn't as simple as with Ext core to handle fields checks.Code:extField.setValidator(new VTypeValidator(VType.EMAIL));
Hope it helps.
Christophe
good post chris =D>
I was trying to figure out how to validate emails in GXT3 so I updated this but it seems you don't add validators like you could in 2008.
I assume there's a simple way to validate email somehow? I will keep looking.
PHP Code:import java.util.ArrayList;
import java.util.List;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.EditorError;
import com.sencha.gxt.widget.core.client.form.Validator;
import com.sencha.gxt.widget.core.client.form.error.DefaultEditorError;
public class VTypeValidator<T> implements Validator<T> {
private VType type;
public VTypeValidator(VType type){
this.type = type;
}
@Override
public List<EditorError> validate(Editor<T> editor, T value) {
List<EditorError> res = null;
if (value == null || "".equals(value)) {
List<EditorError> errors = new ArrayList<EditorError>();
errors.add(new DefaultEditorError(editor, "Fail", ""));
return errors;
}
return res;
}
}
Update
I think its like this; (GXT 3)
not too sure how to add it yet...PHP Code:RegExValidator myValidator = new RegExValidator("^(\\w+)([-+.][\\w]+)*@(\\w[-\\w]*\\.){1,5}([A-Za-z]){2,4}$", "Email");
just trying to figure out how to add this
.....
update
got it!
PHP Code:RegExValidator myEmailValidator = new RegExValidator("^(\\w+)([-+.][\\w]+)*@(\\w[-\\w]*\\.){1,5}([A-Za-z]){2,4}$", "Email");
email.getValidators().add(myEmailValidator);
Or easier still:
Edit: And if you are using the GWT Editor Framework and get a JSR 303 ConstraintViolation from the server, you can pass that to the editor driver and it will be displayed on the TextField automatically.Code:email.addValidator(new RegExValidator(...));