PDA

View Full Version : TextField validation



sarsipius
11 Jan 2011, 11:06 PM
Hi

I try to code JUnit tests to validate my TextField constraints



public static TextField<String> getCountryField(boolean mandatory,String value) {
TextField<String> field = new TextField<String>();
field.setFieldLabel("Country");
field.setMinLength(Regex.getCountryMinLength());
field.setMaxLength(Regex.getCountryMaxLength());
field.setRegex(Regex.getCountryRegex());
field.setAllowBlank(!mandatory);

TextField<String>.TextFieldMessages fieldError = field.new TextFieldMessages();
fieldError.setRegexText("Some characters are not allowed.");
field.setMessages(fieldError);

field.setValue(value);

return field;
}




public void testGetCountryFieldKoRegex() {
TextField<String> field = MyFormField.getCountryField(false,null);

field.setValue("Foo_Bar");
boolean valid = field.isValid();
String errorMsg = field.getErrorMessage();

assertFalse(valid);
assertEquals("Some characters are not allowed.",errorMsg);
}


This code doesn't work since isValid() returns true even is the regex fails.

Could you please help me?

Thanks

sven
11 Jan 2011, 11:37 PM
How does your regex look like? Can you plesae post all depencies so other people are able to run your code?

sarsipius
11 Jan 2011, 11:39 PM
public static int getCountryMaxLength() { return 500; }
public static int getCountryMinLength() { return 1; }
public static String getCountryRegex() { return "^[a-zA-Z-]+$"; }


When I use the UI I don't have any issue

sarsipius
12 Jan 2011, 2:59 AM
it seems that a Field need to be rendered for being validated so I add the line RootPanel.get().add(field);

sven
12 Jan 2011, 12:09 PM
If not rendered, validtion will be done against "". check getRawValue which gets called by isValid