use of com.opensymphony.xwork2.validator.Validator in project struts by apache.
the class Form method findFieldValidators.
private void findFieldValidators(String name, Class actionClass, String actionName, List<Validator> validatorList, List<Validator> resultValidators, String prefix) {
for (Validator validator : validatorList) {
if (validator instanceof FieldValidator) {
FieldValidator fieldValidator = (FieldValidator) validator;
if (validator instanceof VisitorFieldValidator) {
VisitorFieldValidator vfValidator = (VisitorFieldValidator) fieldValidator;
Class clazz = getVisitorReturnType(actionClass, vfValidator.getFieldName());
if (clazz == null) {
continue;
}
List<Validator> visitorValidators = actionValidatorManager.getValidators(clazz, actionName);
String vPrefix = prefix + (vfValidator.isAppendPrefix() ? vfValidator.getFieldName() + "." : "");
findFieldValidators(name, clazz, actionName, visitorValidators, resultValidators, vPrefix);
} else if ((prefix + fieldValidator.getFieldName()).equals(name)) {
if (StringUtils.isNotBlank(prefix)) {
// fixing field name for js side
FieldVisitorValidatorWrapper wrap = new FieldVisitorValidatorWrapper(fieldValidator, prefix);
resultValidators.add(wrap);
} else {
resultValidators.add(fieldValidator);
}
}
}
}
}
use of com.opensymphony.xwork2.validator.Validator in project struts by apache.
the class RegexFieldValidatorTest method testNoFieldName.
public void testNoFieldName() throws Exception {
MyTestPerson testPerson = new MyTestPerson();
testPerson.setUsername("NoExpression");
RegexFieldValidator validator = new RegexFieldValidator();
validator.setRegex("^Sec.*");
validator.setValidatorContext(new DummyValidatorContext(new Object(), tpf));
validator.setFieldName(null);
validator.setValueStack(ActionContext.getContext().getValueStack());
validator.validate(testPerson);
assertFalse(validator.getValidatorContext().hasErrors());
assertFalse(validator.getValidatorContext().hasActionErrors());
assertFalse(validator.getValidatorContext().hasActionMessages());
assertFalse(validator.getValidatorContext().hasFieldErrors());
}
use of com.opensymphony.xwork2.validator.Validator in project struts by apache.
the class RegexFieldValidatorTest method testArrayOfStringField.
public void testArrayOfStringField() throws Exception {
MyTestPerson testPerson = new MyTestPerson();
testPerson.setFriends(new String[] { "Alice", "Matt" });
RegexFieldValidator validator = new RegexFieldValidator();
validator.setRegex("A([a-zA-Z]*)");
validator.setValidatorContext(new DummyValidatorContext(new Object(), tpf));
validator.setFieldName("friends");
validator.setValueStack(ActionContext.getContext().getValueStack());
validator.setDefaultMessage("Only names starting with A are allowed!");
validator.validate(testPerson);
assertTrue(validator.getValidatorContext().hasErrors());
assertFalse(validator.getValidatorContext().hasActionErrors());
assertFalse(validator.getValidatorContext().hasActionMessages());
assertTrue(validator.getValidatorContext().hasFieldErrors());
assertEquals(1, validator.getValidatorContext().getFieldErrors().size());
assertEquals(1, validator.getValidatorContext().getFieldErrors().get("friends").size());
assertEquals("Only names starting with A are allowed!", validator.getValidatorContext().getFieldErrors().get("friends").get(0));
}
use of com.opensymphony.xwork2.validator.Validator in project struts by apache.
the class RegexFieldValidatorTest method testListOfStringField.
public void testListOfStringField() throws Exception {
MyTestPerson testPerson = new MyTestPerson();
testPerson.setCars(Arrays.asList("Audi", "BMW"));
RegexFieldValidator validator = new RegexFieldValidator();
validator.setRegex("A([a-zA-Z]*)");
validator.setValidatorContext(new DummyValidatorContext(new Object(), tpf));
validator.setFieldName("cars");
validator.setValueStack(ActionContext.getContext().getValueStack());
validator.setDefaultMessage("Only cars starting with A are allowed!");
validator.validate(testPerson);
assertTrue(validator.getValidatorContext().hasErrors());
assertFalse(validator.getValidatorContext().hasActionErrors());
assertFalse(validator.getValidatorContext().hasActionMessages());
assertTrue(validator.getValidatorContext().hasFieldErrors());
assertEquals(1, validator.getValidatorContext().getFieldErrors().size());
assertEquals(1, validator.getValidatorContext().getFieldErrors().get("cars").size());
assertEquals("Only cars starting with A are allowed!", validator.getValidatorContext().getFieldErrors().get("cars").get(0));
}
use of com.opensymphony.xwork2.validator.Validator in project struts by apache.
the class RegexFieldValidatorTest method testGetExpression.
public void testGetExpression() {
RegexFieldValidator validator = new RegexFieldValidator();
validator.setRegex("^Hello.*");
assertEquals("^Hello.*", validator.getRegex());
}
Aggregations