Search in sources :

Example 6 with Validator

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);
                }
            }
        }
    }
}
Also used : VisitorFieldValidator(com.opensymphony.xwork2.validator.validators.VisitorFieldValidator) VisitorFieldValidator(com.opensymphony.xwork2.validator.validators.VisitorFieldValidator) VisitorFieldValidator(com.opensymphony.xwork2.validator.validators.VisitorFieldValidator)

Example 7 with Validator

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());
}
Also used : RegexFieldValidator(com.opensymphony.xwork2.validator.validators.RegexFieldValidator)

Example 8 with Validator

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));
}
Also used : RegexFieldValidator(com.opensymphony.xwork2.validator.validators.RegexFieldValidator)

Example 9 with Validator

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));
}
Also used : RegexFieldValidator(com.opensymphony.xwork2.validator.validators.RegexFieldValidator)

Example 10 with Validator

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());
}
Also used : RegexFieldValidator(com.opensymphony.xwork2.validator.validators.RegexFieldValidator)

Aggregations

ValueStack (com.opensymphony.xwork2.util.ValueStack)19 DummyValidatorContext (com.opensymphony.xwork2.validator.DummyValidatorContext)18 ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)18 URLValidator (com.opensymphony.xwork2.validator.validators.URLValidator)16 RegexFieldValidator (com.opensymphony.xwork2.validator.validators.RegexFieldValidator)12 EmailValidator (com.opensymphony.xwork2.validator.validators.EmailValidator)9 ValueStackFactory (com.opensymphony.xwork2.util.ValueStackFactory)7 RequiredStringValidator (com.opensymphony.xwork2.validator.validators.RequiredStringValidator)6 HashMap (java.util.HashMap)6 ActionSupport (com.opensymphony.xwork2.ActionSupport)5 Validator (com.opensymphony.xwork2.validator.Validator)5 ExpressionValidator (com.opensymphony.xwork2.validator.validators.ExpressionValidator)5 VisitorFieldValidator (com.opensymphony.xwork2.validator.validators.VisitorFieldValidator)5 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)4 RequiredFieldValidator (com.opensymphony.xwork2.validator.validators.RequiredFieldValidator)4 StringLengthFieldValidator (com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator)4 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)3 ConversionErrorFieldValidator (com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)3 DateRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator)3 DoubleRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator)3