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();
}
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]);
}
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());
}
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());
}
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));
}
Aggregations