Search in sources :

Example 1 with ValidatorContext

use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.

the class VisitorFieldValidator method validateObject.

private void validateObject(String fieldName, Object o, String visitorContext) throws ValidationException {
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(o);
    ValidatorContext validatorContext;
    if (appendPrefix) {
        ValidatorContext parent = getValidatorContext();
        validatorContext = new AppendingValidatorContext(parent, createTextProvider(o, parent), fieldName, getMessage(o));
    } else {
        ValidatorContext parent = getValidatorContext();
        CompositeTextProvider textProvider = createTextProvider(o, parent);
        validatorContext = new DelegatingValidatorContext(parent, textProvider, parent);
    }
    actionValidatorManager.validate(o, visitorContext, validatorContext);
    stack.pop();
}
Also used : DelegatingValidatorContext(com.opensymphony.xwork2.validator.DelegatingValidatorContext) ValueStack(com.opensymphony.xwork2.util.ValueStack) DelegatingValidatorContext(com.opensymphony.xwork2.validator.DelegatingValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) CompositeTextProvider(com.opensymphony.xwork2.CompositeTextProvider)

Example 2 with ValidatorContext

use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.

the class SimpleActionValidationTest method testMessageKeyIsReturnedIfNoOtherDefault.

public void testMessageKeyIsReturnedIfNoOtherDefault() throws ValidationException {
    Validator validator = new ValidatorSupport() {

        public void validate(Object object) throws ValidationException {
            addActionError(object);
        }
    };
    validator.setValueStack(ActionContext.getContext().getValueStack());
    String messageKey = "does.not.exist";
    validator.setMessageKey(messageKey);
    SimpleAction action = new SimpleAction();
    container.inject(action);
    ValidatorContext validatorContext = new DelegatingValidatorContext(action, container.getInstance(TextProviderFactory.class));
    validator.setValidatorContext(validatorContext);
    validator.validate(this);
    assertTrue(validatorContext.hasActionErrors());
    Collection<String> errors = validatorContext.getActionErrors();
    assertEquals(1, errors.size());
    assertEquals(messageKey, errors.toArray()[0]);
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) SimpleAction(com.opensymphony.xwork2.SimpleAction) ValidatorSupport(com.opensymphony.xwork2.validator.validators.ValidatorSupport)

Example 3 with ValidatorContext

use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.

the class ConversionErrorFieldValidatorTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    ValueStack stack = ActionContext.getContext().getValueStack();
    Map<String, ConversionData> conversionErrors = new HashMap<>();
    conversionErrors.put("foo", new ConversionData("bar", Integer.class));
    ActionContext.of(stack.getContext()).withConversionErrors(conversionErrors).bind();
    validator = new ConversionErrorFieldValidator();
    validationAware = new ValidationAwareSupport();
    DelegatingValidatorContext validatorContext = new DelegatingValidatorContext(validationAware, container.getInstance(TextProviderFactory.class));
    stack.push(validatorContext);
    validator.setValidatorContext(validatorContext);
    validator.setFieldName("foo");
    validator.setValueStack(ActionContext.getContext().getValueStack());
    assertEquals(0, validationAware.getFieldErrors().size());
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData) ConversionErrorFieldValidator(com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)

Example 4 with ValidatorContext

use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.

the class DateRangeFieldValidatorTest method testPassValidation.

public void testPassValidation() throws Exception {
    // given
    ValidationAction action = prepareAction(createDate(2013, 6, 6));
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    DateRangeFieldValidator validator = prepareValidator(action, context);
    // when
    validator.validate(action);
    // then
    assertEquals(0, context.getFieldErrors().size());
}
Also used : DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Example 5 with ValidatorContext

use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.

the class IntRangeFieldValidatorTest method testArrayOfIntValidation.

public void testArrayOfIntValidation() throws Exception {
    // given
    ValidationAction action = new ValidationAction();
    action.setInts(new Integer[] { 99, 100, 101, 102 });
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    IntRangeFieldValidator validator = prepareValidator(action, context);
    // when
    validator.setMin(100);
    validator.setMax(101);
    validator.setFieldName("ints");
    validator.setDefaultMessage("Max is ${max}, min is ${min} but value is ${currentValue}");
    validator.validate(action);
    // then
    assertEquals(1, context.getFieldErrors().size());
    assertEquals(2, context.getFieldErrors().get("ints").size());
    assertEquals("Max is 101, min is 100 but value is 99", context.getFieldErrors().get("ints").get(0));
    assertEquals("Max is 101, min is 100 but value is 102", context.getFieldErrors().get("ints").get(1));
}
Also used : DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Aggregations

ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)21 DummyValidatorContext (com.opensymphony.xwork2.validator.DummyValidatorContext)17 ValueStack (com.opensymphony.xwork2.util.ValueStack)10 List (java.util.List)6 AnnotationUser (com.opensymphony.xwork2.test.AnnotationUser)4 ValueStackFactory (com.opensymphony.xwork2.util.ValueStackFactory)4 DelegatingValidatorContext (com.opensymphony.xwork2.validator.DelegatingValidatorContext)4 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)3 AnnotatedTestBean (com.opensymphony.xwork2.AnnotatedTestBean)2 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)2 ActionContext (com.opensymphony.xwork2.ActionContext)1 CompositeTextProvider (com.opensymphony.xwork2.CompositeTextProvider)1 ModelDriven (com.opensymphony.xwork2.ModelDriven)1 SimpleAction (com.opensymphony.xwork2.SimpleAction)1 ValidationAwareSupport (com.opensymphony.xwork2.ValidationAwareSupport)1 VisitorValidatorTestAction (com.opensymphony.xwork2.validator.VisitorValidatorTestAction)1 ConversionErrorFieldValidator (com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)1 ValidatorSupport (com.opensymphony.xwork2.validator.validators.ValidatorSupport)1 AppendingValidatorContext (com.opensymphony.xwork2.validator.validators.VisitorFieldValidator.AppendingValidatorContext)1 HashMap (java.util.HashMap)1